骨干模型保存示例

时间:2011-12-12 13:09:34

标签: javascript ruby-on-rails backbone.js

我已经生成了一个列表但是我在保存到模型时遇到了问题。

createOnEnter: function(e) {
    var self = this;
    var input = this.$("#new-title");
    var input2 = this.$("#new-content");
    //var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";
    if (!input || e.keyCode != 13) return;
    Mynote.save({title: this.input.val(), content: this.input2.val() }, {
        success: function(model, resp) {
            new LibraryView.Notice({message: msg});

            self.model = model;
            self.render();
            self.delegateEvents();

            Backbone.history.saveLocation('mynotes/' + model.id);
        },
        error: function() {
            new LibraryView.error();
        }

    });

    return false;

},

我这样做了吗?它在集合或“索引”网址的相同视图中,还是我需要为新模型指定不同的路径?

1 个答案:

答案 0 :(得分:8)

相反Mynote.save你应该有一些代码:

var note = new Mynote();
note.save({ tile: ..., content: .. }, { success: ..., error: ..});

请参阅http://documentcloud.github.com/backbone/#Model-save