lundi 22 décembre 2014

ember.js RESTAdapter Request

I made several models (below are 2 of them: category and product) each one of them gets the data using REST Requests.



var Category = DS.Model.extend({
name: DS.attr('string'),
img: DS.attr('string'),
url: DS.attr('string'),
cnt: DS.attr('number'),

parent_id: DS.belongsTo('category', {
// inverse: 'children'
}),
children: DS.attr()
});

var Product = DS.Model.extend({
cat_id: DS.belongsTo('category'),
products: DS.attr()
});


router.js



Router.map(function() {
this.route('categories');
this.resource('category', { path: 'categories/:category_id' }, function() { });
});


routes/categories.js



export default Ember.Route.extend({
model: function() {
var store = this.store;
return Ember.ArrayProxy.create({
categories: store.find('category')
});
},
setupController: function(controller, model) {
controller.set('categories', model.categories);
}
});


routes/category.js



export default Ember.Route.extend({
model: function(params) {
var store = this.store;
var param_id = params.category_id;
var category = store.find('category', param_id);

return Ember.RSVP.hash({
category: category,
filters: store.find('catfilter', param_id),
products: store.find('product', param_id)
});
},
setupController: function(controller, models) {
controller.set('category', models.category);
controller.set('filters', models.filters);
controller.set('products', models.products);
}
});


templates/categories.hbs



{{#each category in categories}}
<li class="ProductGrid-item">
{{#link-to 'category' category class=""}}
<div class="CategoryNav-title">{{category.name}}</div>
<img {{bind-attr src="category.img" alt="category.name"}} width="140" height="140">
</div>
{{/link-to}}
</li>
{{/each}}


The problem is that when I go to dev:4200/categories it makes the request to api/categories and loads the data into the controller and it's available in the template. When I click on the link generated with {{#link-to 'category' category class=""}} from the categories.hbs template it takes me to dev:4200/category/1 but it doesn't make a new request to api/categories/1 or to api/products/1. If I hit F5 (refresh the page) it will make both requests and I can use the json response in the CategoryController which loads category.hbs template.


Also the data loaded from categories I use it in the menu (it's on every page) so is there a way to have it loaded on the first url requested like dev:4200/ and if the user then goes to /categories the controller can fetch the data already available without making a new request for the json?


I'm using ember-cli manually updated with:



DEBUG: Ember : 1.9.0
vendor.js:17480 DEBUG: Ember Data : 1.0.0-beta.12
vendor.js:17480 DEBUG: Handlebars : 2.0.0
vendor.js:17480 DEBUG: jQuery : 1.11.2




Aucun commentaire:

Enregistrer un commentaire