Created
April 20, 2012 12:56
-
-
Save datafatmunger/2428301 to your computer and use it in GitHub Desktop.
Solution Backbone Model
This file contains 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
Solution = Backbone.Model.extend ({ | |
fetchAll: function(options) { | |
var solution = this; | |
var error = false; | |
var errorHandler = function() { | |
error = true; | |
} | |
$.when(solution.comments.fetch({ error: errorHandler }), | |
solution.likes.fetch({ error: errorHandler }), | |
solution.team.fetch({ error: errorHandler }), | |
solution.assets.fetch({ error: errorHandler })).done(function(a, b, c, d) { | |
if(options.success && !error) | |
options.success(); | |
else if(options.error) | |
options.error(); | |
}); | |
}, | |
initialize: function() { | |
this.comments = new Comments(); | |
this.comments.url = this.url() + '/solution_comments'; | |
this.likes = new Likes(); | |
this.likes.url = this.url() + '/solution_likes'; | |
this.team = new Team(); | |
this.team.url = this.url() + '/team_memberships'; | |
this.assets = new Assets(); | |
this.assets.url = this.url() + '/solution_assets'; | |
}, | |
like: function(callback) { | |
var f = function() { | |
like = this.likes.get(this.get('like_id')); | |
solution = this; | |
if(like) { | |
like.url = this.likes.url + '/' + like.get('id'); | |
like.destroy({ | |
success: function(model, response) { | |
this.like_id = null; | |
solution.likes.remove(model); | |
if(callback) | |
callback(); | |
} | |
}); | |
} else { | |
like = new Like(); | |
like.url = this.likes.url; | |
like.save(null, { | |
success: function(model, response) { | |
this.like_id = model.id; | |
solution.likes.add(model); | |
if(callback) | |
callback(); | |
} | |
}); | |
} | |
}; | |
if(!this.likes) { | |
this.likes.fetch.call({ | |
success: function() { | |
f.call(this); | |
} | |
}, this); | |
} else { | |
f.call(this); | |
} | |
} | |
}); | |
Solutions = Pagable.extend({ | |
model: Solution | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment