How to call the method of extracting the identifier of the passage of the baseline

I want to run a method fetchin the Backbone Collection that will pass an Id parameter similar to what happens inModel.fetch(id)

eg.

var someFoo= new Foo({id: '1234'});// Where Foo is a Backbone Model
someFoo.fetch();

Collection of my magic: -

var tasks = backbone.Collection.extend({
    model: taskModel,
    url: '/MyController/GetTasks',
    initialize: function () {
        return this;
    }
});

In my view, when I try to extract data: -

var _dummyId = 10; //

// Tried approach 1 && It calls an api without any `id` parameter, so I get 500 (Internal Server Error).
this.collection.fetch(_dummyId); 


// Tried approach 2 && which fires API call passing Id, but just after that 
// I am getting error as below:- Uncaught TypeError: object is not a function
this.collection.fetch({
    data: {
        id: _dummyId
    }
});

Found it very late: to interrupt this story, I want something like Get / collection / id in the trunk .

+4
source share
2 answers

Thanks for your answers, finally, I got a solution from the options of the Backbone.js collection .

I apologize that I could not correctly explain the issue, and for the same requirements others did brilliantly and deftly.

: - : -

var Messages = Backbone.Collection.extend({
  initialize: function(models, options) {
    this.id = options.id;
  },
  url: function() {
    return '/messages/' + this.id;
  },
  model: Message,
});

var collection = new Messages([], { id: 2 });
collection.fetch();

nrabinowitz.

+9

Matt Ball, : fetch() Collection , fetch() , .

, - Collection.fetch() (, ), "" , , id (+ , , )... . - :

this.collection = new taskCollection();
newTask = this.collection.add({id: 15002});
newTask.fetch();

, , , , , "ID", collection.fetch()?

+1

All Articles