-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnested.js
119 lines (99 loc) · 3.56 KB
/
nested.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
107
108
109
110
111
112
113
114
115
116
117
118
119
describe('NestedViews', function() {
describe('rendering', function() {
it('should render a nested view', function() {
window.MyNestedView = Backbone.View.extend({
render: function() {
this.$el.html('Hello World!');
}
});
var MyView = HandlebarsView.extend({
template: '{{view "MyNestedView"}}'
});
var view = new MyView();
view.render();
view.$el.html().should.equal('<div>Hello World!</div>');
delete window.MyNestedView;
});
it('should pass helper\'s options to the nested view', function() {
window.MyNestedView = Backbone.View.extend({
initialize: function() {
this.options.should.deep.equal({foo: 'bar', name: 'World'});
},
render: function() {
this.$el.html('Hello ' + this.options.name + '!');
}
});
var MyView = HandlebarsView.extend({
template: '{{view "MyNestedView" name=name foo="bar"}}'
});
var view = new MyView({model: {name: 'World'}});
view.render();
view.$el.html().should.equal('<div>Hello World!</div>');
delete window.MyNestedView;
});
});
describe('view class resolution', function() {
it('should directly instantiate a view class available in the context', function() {
var MyNestedView = Backbone.View.extend({
render: function() {
this.$el.html('Hello World!');
}
});
var MyView = HandlebarsView.extend({
template: '{{view MyNestedView}}'
});
var view = new MyView({model: {MyNestedView: MyNestedView}});
view.render();
view.$el.html().should.equal('<div>Hello World!</div>');
});
it('should directly return a view class', function(done) {
var MyView = Backbone.View.extend();
HandlebarsView.prototype.resolveViewClass(MyView, function(viewClass) {
viewClass.should.equal(MyView);
done();
});
});
it('should look for a view class in the window global object', function(done) {
window.MyView = Backbone.View.extend();
HandlebarsView.prototype.resolveViewClass('MyView', function(viewClass) {
viewClass.should.equal(MyView);
delete window.MyView;
done();
});
});
it('should look for a view class in a nested path of the window global object', function(done) {
window.My = {Deep: {}};
window.My.Deep.View = Backbone.View.extend();
HandlebarsView.prototype.resolveViewClass('My.Deep.View', function(viewClass) {
viewClass.should.equal(My.Deep.View);
delete window.My;
done();
});
});
it('should require the view by its name if not found in the global window object', function(done) {
var MyViewToRequire = Backbone.View.extend();
window.require = function() {
arguments.length.should.equal(2);
arguments[0].should.deep.equal(['MyViewToRequire']);
var callback = arguments[1];
callback.should.be.an.instanceOf(Function);
setTimeout(function() {
callback.call(window, MyViewToRequire);
}, 1);
};
HandlebarsView.prototype.resolveViewClass('MyViewToRequire', function(viewClass) {
viewClass.should.equal(MyViewToRequire);
delete window.MyView;
done();
});
delete window.require;
});
it('should throw an error for an unresolvable view', function() {
var MyView = HandlebarsView.extend({
template: '{{view "MyNestedView"}}'
});
var view = new MyView();
view.render.should.throw(Error);
});
});
});