Getting model length


#1

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


#2

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.


#3

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


#4

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!


#5

Can you post your route here?


#6

My Body route is empty.nothing currently in it.


#7

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:

  1. 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.

  2. 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.


#8

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.


#9

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. =)


#10

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


#11

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 =)


#12

setupController(controller, model) { this._super(...arguments); controller.set('modelLength', model.get('length')); }


#13

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')); }
});

#14

You should also be able to do your length calculation as a computed property in your controller …


#15

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 }}

#16

Damn you telling it after one week .
best solution.


#17

Damn you? :joy:

One week earlier I told you the same, read point (2).


#18

anyway thank you lot .:wink: