Reputation: 2136
I need some suggestions around what should be the best-practice for the below scenario :
1. *Who (Model or View ) should hold the promise when a "FETCH" is called upon the Model ?*
For example :
Model FOO --> initialize: { .....this.promise = this.fetch(); }
View view --> someWhere: { var foo=new Foo(); foo.promise.done(..do something here) }
OR
Model FOO --> initialize: { ... }
View view --> someWhere: { var foo=new Foo();
var myPromise=foo.fetch();myPromise.done(..do something..) }
....
2. Second Question is around whether Initializing a Model
should automatically call the fetch ? ( As noted in the above Example )
There is no harm in doing either ways but I am not able to figure pros-cons of over each either style. Your opinion is appreciated
Upvotes: 0
Views: 32
Reputation: 7195
All anwers are in the official docs.
1) You don't need to hold that promise unless you going to use it later in other methods (which looks strange).
fetch
method returns a jqXHR
which is exactly a promise (Deferred). You can use it immediately in the place where you call fetch
without holding a promise instance.
E.g.:
model.fetch().done(..do something here..);
2) Quote from the docs:
When your app first loads, it's common to have a set of initial models that you know you're going to need, in order to render the page. Instead of firing an extra AJAX request to
fetch
them, a nicer pattern is to have their data already bootstrapped into the page. You can then usereset
to populate your collections with the initial data.
Upvotes: 1