javascript 骨干模型保存示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8474829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:33:52  来源:igfitidea点击:

backbone model save example

javascriptruby-on-railsbackbone.js

提问by user901790

i have generated a list but i have problems saving to the model.

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

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;

},

Did i do this correctly? its in the same view for the collection or 'index' url or do i need to specify a different route for the new model?

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

回答by luacassus

Instead Mynote.saveyou should have something line that:

相反,Mynote.save你应该有一些东西:

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

See http://documentcloud.github.com/backbone/#Model-save

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