-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.modelbinding.nested.js
116 lines (103 loc) · 3.59 KB
/
backbone.modelbinding.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
/*
// Backbone.ModelBinding.Nested 0.3.0
//
// (c) 2012 Jordan Warzecha
// Distributed under the MIT license.
//
// Documentation and full license available at:
// http://github.com/jwarzech/backbone.modelbinding.nested
*/
var _this = this;
Backbone.Model.prototype.setByName = function(key, value, options) {
var setter;
setter = {};
setter[key] = value;
return this.set(setter, options);
};
Backbone.Model.prototype.bindingId = function() {
if (this.id != null) {
return this.id;
} else {
return this.cid;
}
};
Backbone.Collection.prototype.isCollection = function() {
return true;
};
Backbone.Collection.prototype.safeGet = function(id) {
if (this.get(id) != null) {
return this.get(id);
} else {
return this.getByCid(id);
}
};
Backbone.Model.prototype.buildCollection = function(attribute, collection) {
if (this.has(attribute) && this.get(attribute).length > 0) {
this.setByName(attribute, new collection(this.get(attribute)));
} else {
this.setByName(attribute, new collection());
}
return this.get(attribute).parent = this;
};
Backbone.Model.prototype.safeGet = function(attribute) {
return this.get(attribute);
};
Backbone.ModelBinding.parseNestedTokens = function(entity, tokens) {
var attribute, id, result;
id = tokens.shift();
entity = entity.safeGet(id);
attribute = tokens.shift();
if (entity.safeGet(attribute) && (entity.safeGet(attribute).isCollection != null)) {
result = Backbone.ModelBinding.parseNestedTokens(entity.safeGet(attribute), tokens);
entity = result.entity;
attribute = result.attribute;
}
return {
entity: entity,
attribute: attribute
};
};
Backbone.ModelBinding.nestedHandler = {
bind: function(selector, view, model) {
return view.$(selector).each(function(index, value) {
var attribute, entity, isCheckbox, result, tokens,
_this = this;
tokens = value.id.split("-");
if (tokens.length === 1) {
entity = model;
attribute = _.first(tokens);
} else {
entity = model.safeGet(tokens.shift());
result = Backbone.ModelBinding.parseNestedTokens(entity, tokens);
entity = result.entity;
attribute = result.attribute;
}
isCheckbox = $(this).prop("tagName") === 'INPUT' && $(this).prop("type") === 'checkbox';
$(this).change(function() {
if (isCheckbox && $(_this).prop('checked') !== entity.safeGet(attribute)) {
value = $(_this).prop('checked');
} else if ($(_this).val() !== entity.safeGet(attribute)) {
value = $(_this).val();
}
if (value != null) return entity.setByName(attribute, value);
});
entity.bind("change:" + attribute, function() {
if (isCheckbox && entity.safeGet(attribute) !== $(_this).prop('checked')) {
return $(_this).prop('checked', entity.safeGet(attribute));
} else if (entity.safeGet(attribute) !== $(_this).val()) {
return $(_this).val(entity.safeGet(attribute));
}
});
if (isCheckbox) {
return $(this).prop('checked', entity.safeGet(attribute));
} else {
return $(this).val(entity.safeGet(attribute));
}
});
}
};
Backbone.ModelBinding.Conventions.text.handler = Backbone.ModelBinding.nestedHandler;
Backbone.ModelBinding.Conventions.textarea.handler = Backbone.ModelBinding.nestedHandler;
Backbone.ModelBinding.Conventions.password.handler = Backbone.ModelBinding.nestedHandler;
Backbone.ModelBinding.Conventions.checkbox.handler = Backbone.ModelBinding.nestedHandler;
Backbone.ModelBinding.Conventions.select.handler = Backbone.ModelBinding.nestedHandler;