This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
/
model.js
106 lines (93 loc) · 2.91 KB
/
model.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
describe('model', function() {
it("shouldFetch", function() {
var options = {fetch: true};
var a = new (Thorax.Model.extend())();
expect(a.shouldFetch(options)).to.not.be.ok;
var b = new (Thorax.Model.extend({urlRoot: '/'}))();
expect(b.shouldFetch(options)).to.be['true'];
var c = new (Thorax.Model.extend({urlRoot: '/'}))();
c.set({key: 'value'});
expect(c.shouldFetch(options)).to.not.be.ok;
var d = new (Thorax.Collection.extend())();
expect(d.shouldFetch(options)).to.not.be.ok;
var e = new (Thorax.Collection.extend({url: '/'}))();
expect(e.shouldFetch(options)).to.be['true'];
var f = new (Thorax.Collection.extend({url: '/'}))();
expect(e.shouldFetch({fetch: false})).to.be['false'];
});
it("model view binding", function() {
var modelA = new Thorax.Model({letter: 'a'});
var modelB = new Thorax.Model({letter: 'b'});
var modelC = new Thorax.Model({letter: 'c'});
var a = new Thorax.View({
template: '<li>{{letter}}</li>',
model: modelA
});
expect(a.el.firstChild.innerHTML).to.equal('a', 'set via constructor');
var b = new Thorax.View({
template: '<li>{{letter}}</li>'
});
b.setModel(modelB);
expect(b.el.firstChild.innerHTML).to.equal('b', 'set via setModel');
modelB.set({letter: 'B'});
expect(b.el.firstChild.innerHTML).to.equal('B', 'update attribute triggers render');
modelB.set({letter: 'b'});
var c = new Thorax.View({
template: '<li>{{letter}}</li>'
});
c.setModel(modelC, {
render: false
});
expect(c.el.firstChild).to.not.exist;
c.render();
expect(c.el.firstChild.innerHTML).to.equal('c', 'manual render');
});
it("isPopulated", function() {
expect((new Thorax.Model()).isPopulated()).to.be['false'];
expect((new Thorax.Model({key: 'value'})).isPopulated()).to.be['true'];
});
it("$.fn.model", function() {
var model = new Thorax.Model({
key: 'value'
});
var view = new Thorax.View({
model: model,
template: '{{key}}'
});
view.render();
expect(view.html()).to.equal('value');
expect(view.$el.model()).to.equal(model);
});
it("model events", function() {
var callCounter = {
all: 0,
test1: 0,
test2: 0
};
var view = new Thorax.View({
template: '',
events: {
model: {
all: function() {
++callCounter.all;
},
test1: 'test1',
test2: function() {
++callCounter.test2;
}
}
},
test1: function() {
++callCounter.test1;
}
});
var model = new Thorax.Model();
view.setModel(model, {fetch: false});
var oldAllCount = Number(callCounter.all);
model.trigger('test1');
model.trigger('test2');
expect(callCounter.all - oldAllCount).to.equal(2);
expect(callCounter.test1).to.equal(1);
expect(callCounter.test2).to.equal(1);
});
});