Hi there,
simple challenge,
I got a model that I want the length of it and I already tried the
this.store.all('body').get('length');
but it didn’t work for me
How can I get the model length and where should I write the codes ? thanks
this.store.all('body')
In the route returns a promise. What you need is probably a controller property like this.get('model.length')
or use the hook afterModel() { return this.modelFor('body').get('length'); }
in your route.
Good suggestions, remember that (1) afterModel
already has the resolved model passed in as argument, and (2) OP probably needs the length in his template and by then the model will be resolved so model.length
will work in the template
thanks guys .
but it still doesn’t work.
I wrote down length:this.get('model.length')
in my Body model .
and {{length}}
in my Body template but it’s coming up with nothing!
Can you post your route here?
My Body route is empty.nothing currently in it.
If you have wrote it in the route, it will not be available in the template, since route objects are not accessible directly in the template like controller… You have 2 ways to resolve this:
-
Declare it in the model in the afterModel hook, then you can call it by {{model.modelLength}} - assuming you’ve created a modelLength property in your ember RSVP model.
-
Declare it in the controller file or by setupController hook in the route file, so you can call it simply by {{modelLength}}
This should work.
Uh , after all it only worked this way, I wrote down this code :
setupController: function(controller, model) { controller.set('length',this.get('model.length')); }
in my Body route and it gave me the number 2 ! which is wrong.
maybe I wasn’t clear in my questioning .
I have Body record which there are some names in it (currently 5 names) I just want to get this number (damn 5) but it gave me 2 instead.
Can you post here your model hook?
If you are using Ember.RSVP.hash, it will return the number of properties inserted in the hash object, not the actual results you may looking for.
One more thing: Try to avoid using common names as properties, like “length”… You may bump into some conflict sooner or later. =)
Body model :
export default Model.extend({ name: attr('string'), day: attr('number'), month: attr('number') });
Body route :
export default Ember.Route.extend({ setupController: function(controller, model) { controller.set('the_ength',this.get('model.length')); } });
and Body controller is empty
Actually, we need to know about your model hook in the route… like this:
model(){
return Ember.RSVP.hash({
yourData: this.store.findAll('yourModel'),
otherData: this.findRecord('otherModel')
}
});
For this example above, to get the length from yourData
, your setupController should look like this:
setupController: function(controller, model) {
controller.set('length',this.get('model.yourData.length'));
}
and in the Template:
{{length}}
PS.: i’ve created another user (there is no way to change my user), but i’m @rael.venturus =)
setupController(controller, model) { this._super(...arguments); controller.set('modelLength', model.get('length')); }
after nearly 5 days it finally worked.
just writing down the way it worked for those who may need:
export default Ember.Route.extend({
model(){
return this.store.findAll('Body')
},
setupController(controller, model) { controller.set('modelLength', model.get('length')); }
});
You should also be able to do your length calculation as a computed property in your controller …
You don’t need to call setupController
. Only by loading data:
export default Ember.Route.extend({
model(){
return this.store.findAll('Body')
},
you can then do the following in your template:
{{ model.length }}
Damn you telling it after one week .
best solution.
Damn you?
One week earlier I told you the same, read point (2).
anyway thank you lot .