找到你要的答案

Q:Cannot get the result of the Backbone model fetch

Q:无法获取主干模型取的结果

My model urlRoot:

    urlRoot: function() {
    if (this.id != null ) {
        return 'notes/' + this.id; 
    } else return 'notes';
}

Function:

window.show_note = function (note_id) {
var note = new Memo.Models.Note([], { id: note_id });
note.fetch({
    success: function (collection, note, response) {
        var noteObj = collection.get("0");
        var noteView = new Memo.Views.FullNote( {model: noteObj }, {flag: 0 } );    
        $('.content').html(noteView.render().el);
    }
});}

{ id: note_id } - I post this to server to get note by id I want to do 'set' or 'get' functions on model 'note' after note.fetch in a callback function - success, but only I have is error: 'Uncaught TypeError: note.set is not a function'.

If I do this way: 'var noteObj = collection.get("0");' I get that I need but I still can`t use get or set.

我的模型urlroot:

    urlRoot: function() {
    if (this.id != null ) {
        return 'notes/' + this.id; 
    } else return 'notes';
}

功能:

window.show_note = function (note_id) {
var note = new Memo.Models.Note([], { id: note_id });
note.fetch({
    success: function (collection, note, response) {
        var noteObj = collection.get("0");
        var noteView = new Memo.Views.FullNote( {model: noteObj }, {flag: 0 } );    
        $('.content').html(noteView.render().el);
    }
});}

{ id: note_id } - I post this to server to get note by id I want to do 'set' or 'get' functions on model 'note' after note.fetch in a callback function - success, but only I have is error: 'Uncaught TypeError: note.set is not a function'.

If I do this way: 'var noteObj = collection.get("0");' I get that I need but I still can`t use get or set.

answer1: 回答1:

You should set urlRoot to:

urlRoot: '/notes'

And backbone will figure out that it needs to add the id to the url. (docs)

Assuming Memo.Models.Note is a model and not a collection, the above snippet should be like this:

window.show_note = function(note_id) {
    var note = new Memo.Models.Note({ id: note_id });

    note.fetch({
        success: function (model, response, options) {
            var noteView = new Memo.Views.FullNote({
                model: model
            }, {flag: 0 });

            $('.content').html(noteView.render().el);
        }
    });
};

Note the argument passed to new Memo.Models.Note. A backbone model constructor takes two arguments: attributes and options (docs) as opposed to a collection, which takes models and options (docs). So you'll want to add the hash with the id property as the first argument.

Also note the function signature of the success callback. For a model the success callback takes three arguments: model, response and options (docs). You'll be interested in the model argument because that is the fetched backbone model. response is the raw response data.

I hope my assumptions are right and this is the answer you are looking for.

你应该urlroot来:

urlRoot: '/notes'

和骨干会发现,它需要添加ID的网址。(文档)

假设memo.models.note是模型而不是收藏,上面的代码片段应该是这样的:

window.show_note = function(note_id) {
    var note = new Memo.Models.Note({ id: note_id });

    note.fetch({
        success: function (model, response, options) {
            var noteView = new Memo.Views.FullNote({
                model: model
            }, {flag: 0 });

            $('.content').html(noteView.render().el);
        }
    });
};

注意参数传递给新的memo.models.note。一个骨架模型的构造函数接受两个参数:属性和选项(文件)而不是一个集合,它以模型和期权(文档)。因此,您将希望将id属性的哈希值添加为第一个参数。

还注意到成功回调的函数签名。一个模型的成功回调需要三个参数:模型、反应和选项(文档)。您将对模型参数感兴趣,因为这是抽取的主干模型。响应是原始响应数据。

我希望我的假设是正确的,这就是你要找的答案。

javascript  backbone.js