Created
June 20, 2014 11:08
-
-
Save ChanderG/a5f2581e413c2b94fd00 to your computer and use it in GitHub Desktop.
Many-Many relations EmberData (Fixture adapters)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[add your bin description]" /> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
<script src="http://code.jquery.com/jquery-2.0.2.js"></script> | |
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script> | |
<script src="http://builds.emberjs.com/tags/v1.5.0/ember.js"></script> | |
<script src="http://builds.emberjs.com/beta/ember-data.js"></script> | |
</head> | |
<body> | |
<script type="text/x-handlebars" data-template-name="application"> | |
<h1>Ember Data there?</h1> | |
This is an example of hasMany - hasMany</br> | |
See comments and output</br></br> | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
HERE IS A LIST of Customers: | |
<ul> | |
{{#each}} | |
<li>{{id}} {{name}}</li> | |
<ul> | |
{{#each order in orders}} | |
<li>{{order.name}}</li> | |
{{/each}} | |
</ul> | |
{{/each}} | |
</ul> | |
<button {{action showOrders}}>Show</button> | |
HERE IS A LIST of Orders: | |
<ul> | |
{{#each order in controller.orders}} | |
<li>{{order.id}}{{order.name}}</li> | |
<ul> | |
{{#each cust in order.customers}} | |
<li>{{cust.name}}</li> | |
{{/each}} | |
</ul> | |
{{/each}} | |
</ul> | |
</script> | |
</body> | |
</html> | |
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 = Ember.Application.create({LOG_TRANSITIONS: true}); | |
App.ApplicationAdapter = DS.FixtureAdapter.extend({}); | |
App.Router.map(function() { | |
}); | |
App.Customer = DS.Model.extend({ | |
name: DS.attr('string'), | |
orders: DS.hasMany("order" , {async: true}) //needs async true to load required data on its own | |
}); | |
App.Customer.FIXTURES = [ | |
{ | |
id: 1, | |
name: "Harry", | |
orders: [1,3] | |
}, | |
{ | |
id: 2, | |
name: "Ron", | |
orders: [2,3] | |
} | |
]; | |
App.Order = DS.Model.extend({ | |
name: DS.attr('string'), | |
customers: DS.hasMany('customer') | |
}); | |
App.Order.FIXTURES = [ | |
{ | |
id: 1, | |
name: "Butterbeer", | |
customers: [1] | |
}, | |
{ | |
id: 2, | |
name: "Chocolate Frogs", | |
customers: [2] | |
}, | |
{ | |
id: 3, | |
name: "Liquorice Cakes", | |
customers: [1,2] | |
} | |
]; | |
App.IndexRoute = Ember.Route.extend({ | |
model: function(){ | |
return this.store.find('customer'); | |
} | |
}); | |
App.IndexController = Ember.ArrayController.extend({ | |
orders: null, | |
actions: { | |
showOrders: function(){ | |
console.log('hello there'); | |
this.set('orders', this.get('store').find('order')); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment