Last active
August 29, 2015 14:02
-
-
Save elisechant/814b71f058c13f0932d5 to your computer and use it in GitHub Desktop.
Ember data - exploration in mult-dimensional model relationships. Also read this: http://emberjs.com/blog/2014/03/18/the-road-to-ember-data-1-0.html and this http://www.toptal.com/emberjs/a-thorough-guide-to-ember-data. Things to remember: don't think of client data modelling as 1:1 with database tables, so read this too http://discuss.emberjs.c…
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
/** | |
* Application code which demonstrates how to define multi-dimensional | |
* Ember-data relationships. | |
* | |
* The example uses the following Model concepts: | |
* - Issue (representing an Issue) | |
* - User (representing a User) | |
* - Vote (representing a User's vote for an Issue; a Pivot Model for | |
* Issue and User ) | |
* | |
*/ | |
var App = Ember.Application.create(); | |
App.ApplicationAdapter = DS.FixtureAdapter(); | |
App.Router.map(function() { | |
this.resource('issues'); // Issue model.vote is available here, no need to define a'vote' route | |
this.resource('users'); // User model.vote is available here | |
}) | |
App.IssuesRoute = App.Route.extend({ | |
model: function() { | |
return this.store.find('issue'); | |
} | |
}); | |
App.UsersRoute = App.Route.extend({ | |
model: function() { | |
return this.store.find('user'); | |
} | |
}); |
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
App.Issue = DS.Model.extend({ | |
title: DS.attr('string'), | |
vote: DS.hasMany('vote', { // define a one-to-many relationship; 'vote' param represents App.Vote | |
inverse: 'issue' // specify the inverse property in App.Vote, note: implied would be 'issue' | |
} | |
}); | |
// create some dummy data | |
App.Issue.FIXTURES = [{ | |
id: 1, // don't assign an id on the model; Ember generates and auto-increments this | |
title: 'record one', | |
vote: [1, 2, 3] // array data represents a join; the join values holds a programatic reference to their corresponding fixture - no need to load App.Vote to see values here | |
},{ | |
id: 2, | |
title: 'record two', | |
vote: [3] | |
},{ | |
id: 3, | |
title: 'record three', | |
vote: [] | |
}]; |
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
App.User = DS.Model.extend({ | |
staffNumber: DS.attr() , | |
vote: DS.hasMany('vote', { | |
inverse: 'user' | |
}) | |
}); | |
App.User.FIXTURES = [{ | |
id: 1, | |
staffNumber: '003265326', | |
vote: ['1', '2', '3'] | |
},{ | |
id: 2, | |
staffNumber: '006262626', | |
vote: ['3'] | |
}]; |
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
App.Vote = DS.Model.extend({ | |
issue: DS.belongsTo('issue', { // refers to App.Issue | |
inverse: 'vote' | |
}), | |
user: DS.belongsTo('user', { | |
inverse: 'vote' // optional to specify the inverse property of App.User | |
}), | |
}); | |
App.Vote.FIXTURES = [{ | |
id: 1, // id's must also be defined for joining tables | |
user: 1, | |
issue: 1 | |
},{ | |
id: 2, | |
user: 1, | |
issue: 2 | |
},{ | |
id: 3, | |
user: 1, | |
issue: 3 | |
},{ | |
id: 4, | |
user: 2, | |
issue: 3 | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment