Created
October 30, 2014 16:44
-
-
Save gabriel-dehan/97e374ab340e98bd89f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| }); | |
| }, | |
| ... | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| comments fetched | |
| success for users | |
| (6) fetching comments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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