Mam następujący scenariusz -Aktualizacja widok Backbone gdy model jest zapisywany
window.Wine = Backbone.Model.extend({
urlRoot: '/wines'
});
window.WineCollection = Backbone.Collection.extend({
model: Wine,
url: "/wines"
});
Mam model i odpowiedni zbiór zdefiniowany.
window.WineListView = Backbone.View.extend({
el: '#wineList',
initialize: function() {
this.model.bind("reset", this.render, this);
this.model.bind("add", function (wine) {
alert("model added");
});
},
render: function (eventName) {
_.each(this.model.models, function (wine) {
$(this.el).append(new WineListItemView({
model: wine
}).render().el);
}, this);
return this;
}
});
window.WineListItemView = Backbone.View.extend({
tagName: "li",
initiliaze: function() {
this.render();
},
render: function (eventName) {
var template = _.template($('#wine-list-item').html());
$(this.el).html(template(this.model.toJSON()));
return this;
}
});
Powyższe widoki tworzą indywidualny element listy dla każdego modelu.
window.WineView = Backbone.View.extend({
el: $("#mainArea"),
initialize: function() {
this.render();
},
render: function (eventName) {
var template = _.template($("#wine-details").html(), {});
$(this.el).html(template);
},
events: {
"click #save_form": "save_form"
},
save_form: function() {
var wine = new Wine();
wine.save();
return false;
}
});
window.HeaderView = Backbone.View.extend({
el: '.header',
initialize: function() {
this.render();
},
render: function (eventName) {
var template = _.template($('#header').html());
$(this.el).html(template());
return this;
},
events: {
"click .new": "newWine"
},
newWine: function (event) {
var wine_View = new WineView();
wine_View.render();
}
});
W WineView, po kliknięciu przycisku Wyślij formularza model zostaje zapisany. Funkcja Backbone.sync jest nadpisana.
var AppRouter = Backbone.Router.extend({
routes: {
"": "list"
},
list: function() {
this.wineList = new WineCollection();
this.wineListView = new WineListView({
model: this.wineList
});
this.wineList.fetch();
}
});
var app = new AppRouter();
Backbone.history.start();
var header = new HeaderView();
Moim problemem jest, gdy model jest zapisany WineListView nie dostać odświeżony i pokazać nowy model, który został dodany. Precyzyjnie thismode.bind ("add") nie jest wywoływany.
Przepraszam za szczegółowość pytania. Ale to przeszkadza mi przez prawie tydzień. Każda pomoc jest bardzo doceniana.
Pozdrawiam!
Czyżby typo "initiliaze" ma coś z tym wspólnego? –