View doesn't update when model changes


#1

Hi there,

I meet a problem after i upgrade to latest ember and ember data.

  • ember: 1.8.0-beta.1+canary.44b99c97
  • ember data: 1.0.0-beta.9+canary.2b1f093e2b

Here is my code, same as http://jsfiddle.net/H6Gqf/, but it doesn’t work properly as the code at jsfiddle.

Form Chrome inspector, I notice the comments increasing, but the view doesn’t update.

Can anybody help me? thanks.

<!DOCTYPE HTML>
<html lang="en">
<body>
<script type="text/x-handlebars" data-template-name="index">
    <a href="#" {{action "reloadComments"}}>Reload comments</a>
    {{title}}<br/>
    {{#each comments}}
        {{message}}
    {{/each}}
</script>


<script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="scripts/handlebars-v1.1.2.js"></script>
<script type="text/javascript" src="scripts/ember.js"></script>
<script type="text/javascript" src="scripts/ember-data.js"></script>
<script type="text/javascript" src="jquery.mockjax.js"></script>

<script>
App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
  model: function() {
      return this.store.find('post', 1);
  },
  setupController: function(controller, model) {
      controller.set('content', model);
  },
  actions: {
      reloadComments: function() {
          var post = this.controller.get('model');          
          var commentsPromise = this.controller.get('comments');          
          commentsPromise.then(function(comments) {
              comments.reloadLinks();
          });
      }
  }
});

var get = Ember.get;

DS.ManyArray.reopen({
    reloadLinks: function() {
        var records = get(this, 'content'),
        store = get(this, 'store'),
        owner = get(this, 'owner'),
        type = get(this, 'type'),
        name = get(this, 'name'),
        resolver = Ember.RSVP.defer();
                
        var meta = owner.constructor.metaForProperty(name);
        var link = owner._data.links[meta.key];
        store.findHasMany(owner, link, meta, resolver);
    }
});

App.Post = DS.Model.extend({
    title: DS.attr('string'),
    comments: DS.hasMany('comment', {async: true})
})

App.Comment = DS.Model.extend({
    message: DS.attr('string'),
    post: DS.belongsTo('post')
});

$.mockjax({
    url: '/posts/1',
    response: function(settings) {
        var json = {
            post: {
                id: 1,
                title: 'rails is omakase',
                links: { comments: '/posts/1/comments' }
            }
        };
        settings.success(json);
    }
});

$.mockjax({
    url: '/posts/1/comments',
    response: function(settings) {
        var json = {
            comments: comments
        };
        settings.success(json);
    }
});

var comments = [];
var i = 0;

setInterval(function() {
    i++;
    comments.push({id: i, message: 'Whoa ' + i + '!' })
}, 500);    

</script>
</body>
</html>