यहाँ इस सवाल का मेरा पूरा समाधान है।
- संग्रह contructor इसे करने के लिए पारित डेटा
- जब एक मॉडल IsLocked संपत्ति संग्रह अद्यतन किया जाता है फिर से मान्य करने की
मैं की जरूरत है संभाल करने की जरूरत है: यहाँ कुछ आइटम को हल किया जा करने के लिए आवश्यक है कि मुझे यकीन नहीं है कि संग्रह में बाध्यकारी गुण जोड़ना एक अच्छा विचार है, लेकिन यह पता लगाने में कुछ मजेदार था, और मैंने बहुत कुछ सीखा।
var log = {};//this is for debugging
_.extend(log, Backbone.Events);
var Car = Backbone.Model.extend({});
var Cars = Backbone.Collection.extend({
model: Car,
url: "scripts/data/Cars.json",
initialize: function() {
var _this = this;
this.isValid = false; //user should bind to "change:isValid"
this._isPending = false; //This is so that
this.bind("add", this.modelAdded, this);
this.bind("reset", this.modelsAdded, this);
if (this.length > 0) {
//Model passed in though the constructor will not be binded
//so call modelsAdded
this.modelsAdded(this);
}
},
modelsAdded: function (collection) {
this._isPending = true
log.trigger("log", ["modelsAdded: " + collection.length]);
collection.each(this.modelAdded, this); //Do nut remove "this" param. It need when modelAdded gets called
this._isPending = false
this._validate();
},
modelAdded: function (model) {
log.trigger("log", ["modelAdded: " + model.get("Model") + "; IsLocked: " + model.get("IsLocked")]);
//!!!for each model added to the colleciton bind
//its IsLocked property to the collection modelIsLockedChange function
model.bind("change:IsLocked", this.modelIsLockedChanged, this);
if (this._isPending == false) {
this._validate();
}
},
modelIsLockedChanged: function (model) {
log.trigger("log", ["modelIsLockedChanged:" + model.get("Model") + "; IsLocked: " + model.get("IsLocked")]);
this._validate();
},
_validate: function() {
var isValid = true;
this.each(function (model) {
if (model.get("IsLocked") == false) {
isValid = false
}
});
if (this.isValid != isValid) {
this.isValid = isValid
cars.trigger("change:isValid", [this.isValid])
}
},
});
यह दृश्य डिबगिंग उद्देश्यों के लिए है
var Logger = Backbone.View.extend({
el: $("#output"),
initialize: function(){
log.bind("log", this.logMessage, this)
},
render: function(){
return $(this.el).html("");
},
logMessage: function(message){
$(this.el).find("ul").append("<li>" + message[0] + "</li>");
}
})
निम्नलिखित कोड संग्रह
var logger = new Logger();
log.bind("log", function(message){
console.log(message[0]);
});
var carsData = [
{ "Model": "Chevy Camero", "Year": 1982, "IsLocked": true },
{ "Model": "Ford F-150", "Year": 2011, "IsLocked": false }
]
var cars = new Cars(carsData);
cars.bind("change:isValid", function(isValid){
log.trigger("log", ["change:isValid: " + isValid]);
});
cars.add({ "Model": "Toyota Tacoma", "Year": 2006, "IsLocked": true });
cars.at(1).set({ "IsLocked": true });
परीक्षण करने के लिए प्रयोग किया जाता है
उत्तर के लिए धन्यवाद। मेरा मानना है कि आपने मुझे सही दिशा में इंगित किया है। प्रारंभिक में सत्यापित की जाने वाली जानकारी की सराहना करें। जब मैं अपने कामकाजी समाधान के साथ खत्म करूँगा, तो मैं इसे पोस्ट करूंगा। –