forked from jaredly/treed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickstart.js
50 lines (40 loc) · 1.2 KB
/
quickstart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var React = require('react')
, Treed = require('./classy')
, ListView = require('./views/list')
, extend = require('./util/extend')
module.exports = Treed
Treed.prototype.quickstart = function (el, options) {
options = options || {}
el = ensureElement(el)
this.keyManager.listen(window)
return this.initStore(options.data || {content: ''}, {
actions: options.actions || require('./views/list/actions'),
}).then(() => {
return this.startView(el, options)
}).catch(error => {
console.warn('Treed initialization failed!', error)
throw error
});
}
Treed.prototype.startView = function (el, options) {
options = options || {}
return new Promise((resolve, reject) => {
var viewOptions = extend({
keys: options.keys || ListView.keys,
}, options.viewOptions)
var View = options.View || ListView
, props = this.addView(viewOptions)
React.render(<View {...props}/>, el, function (err) {
if (err) return reject(err)
resolve(props.store)
})
})
}
function ensureElement(el) {
if ('string' === typeof el) {
var found = document.querySelector(el)
if (!found) throw new Error('element not found: ' + el)
el = found
}
return el
}