Skip to content

Instantly share code, notes, and snippets.

@gabriel-dehan
Created October 30, 2014 16:44
Show Gist options
  • Select an option

  • Save gabriel-dehan/97e374ab340e98bd89f3 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-dehan/97e374ab340e98bd89f3 to your computer and use it in GitHub Desktop.
Fluidity.Models.Collect = Backbone.Model.extend({
urlRoot: function() {
return "/api/collects";
},
initialize: function(attr, options) {
this.ideas = new Fluidity.Collections.IdeasCollection();
this.users = new Fluidity.Collections.UsersCollection();
this.workshop = new Fluidity.Models.Workshop({id: this.get('workshop_id')});
// TODO: Defer till needed
this.ideas_promise = this.refreshIdeas();
this.fetchUsers();
},
fetchUsers: function() {
this.users.fetch({
data: $.param({collect_id: this.get('id')}),
success: function() {
console.log("success for users");
},
error: function() {
console.log("error for users");
},
reset: true,
});
},
refreshIdeas: function() {
var self = this;
return this.ideas.fetch({
data: $.param({collect_id: this.get('id')}),
success: function() {
Fluidity.Logger.debug("Collect: ideas.fetch() successed");
},
error: function() {
Fluidity.Logger.error("Collect: ideas.fetch() failed");
},
reset:true
});
},
onFetchIdeasComments: function(callback, context) {
var self = this;
this.ideas_promise.done(function(ideas) {
var promises = _.compact(_.map(self.ideas.models, function(idea) {
if (idea.comments_promise) {
return idea.comments_promise;
}
}));
// We apply when to all promises, using apply as when() expects multiple arguments
$.when(promises[0]).done(callback.call(context));
//$.when.apply($, promises).done(callback.call(context));
});
},
...
});
comments fetched
success for users
(6) fetching comments
Fluidity.Models.Idea = Backbone.Model.extend({
urlRoot: function() {
return "/api/ideas";
},
initialize: function(attr, options) {
this.comments = new Fluidity.Collections.CommentsCollection();
// If this is not a new idea
if (this.get('id')) {
this.comments_promise = this.refreshComments();
}
},
refreshComments: function() {
var self = this;
return this.comments.fetch({
data: $.param({idea_id: this.get('id')}),
success: function(comments) {
console.log('fetching comments');
comments.forEach(function(comment) { comment.set('idea_id', self.get('id')) });
Fluidity.Logger.debug("Idea: comment.fetch() successed");
},
error: function() {
Fluidity.Logger.error("Idea: comment.fetch() failed");
},
reset:true
});
},
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment