Ember Data有幾種內(nèi)置的關(guān)系類型,它們定義了每個模型之間的關(guān)系。
一對一
一對多
多對多
顯式反轉(zhuǎn)
反身關(guān)系
對于兩個模型之間的一對一關(guān)系,請使用DS.belongsTo:
App.User = DS.Model.extend({ profile: DS.belongsTo('profile') }); App.Profile = DS.Model.extend({ user: DS.belongsTo('user') });
對于兩個模型之間的一對多關(guān)系,請使用DS.belongsTo與DS.hasMany的組合:
App.Post = DS.Model.extend({ comments: DS.hasMany('comment') }); App.Comment = DS.Model.extend({ post: DS.belongsTo('post') });
對于兩個模型之間的多對多關(guān)系使用DS.hasMany:
App.Post = DS.Model.extend({ tags: DS.hasMany('tag') }); App.Tag = DS.Model.extend({ posts: DS.hasMany('post') });
對于顯式逆元素,使用DS.hasMany的逆選項指定相關(guān)模型上的哪個屬性為逆:
App.Post = DS.Model.extend({ tags: DS.hasMany('tag') }); App.Tag = DS.Model.extend({ posts: DS.hasMany('post') });
顯式地定義另一側(cè)相應(yīng)地設(shè)置顯式逆,并且如果不需要另一側(cè)設(shè)置反轉(zhuǎn)為null:
App.Comment = DS.Model.extend({ onePost: belongsTo('post'), twoPost: belongsTo('post'), redPost: belongsTo('post'), bluePost: belongsTo('post') }); App.Post = DS.Model.extend({ comments: hasMany('comment', { inverse: 'redPost' }) });
<!DOCTYPE html> <html> <head> <title>Emberjs Defining Relationship</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="index"> <h2>Details of player: </h2> <b>Activity name:</b> {{name}}<br/> {{#each player in team}} <b>Player name: </b>{{player.name}}<br/> <b>Player's country name:</b> {{player.country.name}} {{/each}} </script> <script type="text/javascript"> App = Ember.Application.create(); //defining model App.Country = DS.Model.extend({ name: DS.attr('string'), country: DS.attr('string') }); App.Country.FIXTURES = [{ id: 1, name: 'India'} ]; App.Player = DS.Model.extend({ //data Model name: DS.attr('string'), //one-to-one relation uses DS.belongsTo method country: DS.belongsTo('country', {async: true}) }); //attach fixtures(sample data) to the model's class App.Player.FIXTURES = [{ id: 1, name: 'Sachin Tendulkar', country: 1 }]; //defining model App.Activity = DS.Model.extend({ // when async is true, it will fetch the related entities when you actually request them team: DS.hasMany('player',{async:true}), //one-to-many relation uses DS.hasMany method name: DS.attr('string') }); //attach fixtures(sample data) to the model's class App.Activity.FIXTURES = [{ id: 1, team: [1], name: 'Cricket' }]; //The store cache of all records available in an application App.Store = DS.Store.extend({ //adapter translating requested records into the appropriate calls adapter: DS.FixtureAdapter.extend() }); App.IndexRoute = Ember.Route.extend({ model: function() { //index route return this.store.find('activity', 1); } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在model_retnship.html文件中
在瀏覽器中打開此HTML文件。
更多建議: