Access DS.PromiseArray Value from Handlebars


#1

Hi, I try to access the value of a PromiseArray but i nevver get a value. I asked the question allready in stackoverflow (http://stackoverflow.com/questions/24654112/access-promisearray-directly), but i got no answer.

I try the following, but i got no direct access to the array value with a index, so that {{user.[0].name}} never has a value, but with a each iteration over the array i get the value. What am I doing wrong? Also why is there no error if i raise the index to [999] for example?

App.IndexRoute = Ember.Route.extend({
  model: function(){
    return Ember.Object.create({user: this.store.find('user',{status:"active"}), job: this.store.find('job')});
  }
});

  <script type="text/x-handlebars" data-template-name="index">
    <h2>{{user}},{{user.length}}</h2>

    <h2>{{user.0.name}}</h2>
    <h2>{{user.[0]}}</h2>
    <h2>{{user.[0].name}}</h2>    
    <ul>
      {{#each user}}
        <li>
          <label>{{name}}</label>
        </li>
      {{/each}}
    </ul>
  </script>

I also have a design question regarding ember. If I use the filtered user object only for display purpose, is it correct to put it in the model or should i put it in the controller, if this is possible?


#2

Try using RSVP.hash instead, as suggested here


#3

thanks, but it has no impact: http://jsbin.com/vanipayo/3/edit


#4

You’re using handlebars templates the wrong way. It works well in this example:


#5

Thank you very much! Because I have just started with ember I still have some questions, maybe you can answer some of them :)? So just to clarify, it is not possible to access that array directly? what if i want to access the second element? Why does my handlebars helper not work? Why do I not get an error for user.0. or in other words how should I know how this thing works with almost no documentation and no error messages :)?


#6

I’m not that one of an expert neither :wink:

Anyhow, handlebar design principle is “logic-less”, which means that you can not rely on pure template in order to accomplish someking of operations. (simples if over boolean values and loop operators such as eachare supported anyway)

To answer your question, you can’t access these array directly (through arr[0]), mainly because you can pass parameters to functions…for that you use helpers) but you can access it trough its properties. If there is a firstObject() property you can use it (but objectAtContent (index) wont work).

I agree that there are few errors messages on the template behavior, but documentation is quite good (not extensive, however) here and here


#7

Hi, I still don’t get it, and I don’t find any documentation that helps me. I played around with angular and for me it seems much more like what I expect. Thank you!