Reputation: 35
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.
Upvotes: 1
Views: 128
Reputation: 1793
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.
Upvotes: 2