保存模型时更新Backbone视图

ver*_*ure 8 javascript backbone.js

我有以下情况 -

window.Wine = Backbone.Model.extend({
  urlRoot: '/wines'

});
window.WineCollection = Backbone.Collection.extend({
   model: Wine,
   url: "/wines"
});
Run Code Online (Sandbox Code Playgroud)

我有一个模型和相应的集合定义.

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;
    }
});
Run Code Online (Sandbox Code Playgroud)

以上视图为每个模型创建单独的列表项.

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();
    }
});
Run Code Online (Sandbox Code Playgroud)

在WineView中,单击表单的提交按钮时,将保存模型.Backbone.sync函数被覆盖.

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();
Run Code Online (Sandbox Code Playgroud)

我担心的是,当保存模型时,WineListView不会刷新并显示已添加的新模型.确切地说,不会调用this.mode.bind("add").

我为这个问题的冗长而道歉.但是,这已经困扰了我近一个星期.任何帮助是极大的赞赏.

干杯!

San*_*der 5

从未调用过add的原因正是一旦你的葡萄酒被保存,wine.save();你应该添加一个成功函数并将其添加到winelist集合中

试试这个:

wine.save(wine.toJSON(), {
    success: function(){ 
        // model was saved successfully, now add it to the collection
        yourwinelist.add(wine);
        // if you can't access your wine list from this context, you might want to raise an event, and pass the wine, to catch it somewhere else:
        obj.trigger('myWineAdded', wine); // this can be raised on a global event aggregator
    },
    error: function(){ 
        // handle app when saving to server fails
        alert('failed to save wine');
    }
});
Run Code Online (Sandbox Code Playgroud)

有关事件聚合器概念的更多信息,请检查以下内容:
路由和事件聚合器协调backbone.js中的视图