3

I have an attribute in my model that is an array (of floats) and is serialized and returned as such by my Rails backend. I then take the field, (monthlyData in this example) and plot it using a D3-based component that I wrote.

{{bar-chart dataBinding="monthlyData" height=75 width =300}}

Everything works fine, except when the data changes. In the JS of my component I have an observer that observes the data attribute of the component and re-renders the view, however this observer is not fired if my model changes. It does, however fire if I set the monthlyData field directly, e.g. by doing this.get('model').set('data', [1,2,3,4,5]) in my controller.

I have tried both not specifying an attribute type (i.e. in my model specifying monthlyData: DS.attr() and using a custom array transform (in the hope that the returned Ember.Array would be observer-friendly):

App.ArrayTransform = DS.Transform.extend({
  deserialize: function(serialized) {
    return Ember.A(serialized);
  },
  serialize: function(deserialized) {
    return deserialized.toArray();
  }
});

I'm using Ember 1.3 beta 1 and Ember Data 1.0.0 beta 4. What's the best practice around using arrays as data types and how do I make them observable?

8
  • 1
    Why are you re-rendering the view? Are you sure that the model has updated? Commented Nov 27, 2013 at 4:36
  • I guess updating would be more accurate. I don't re-render, just update the height of the bars in my chart to reflect the new data. That code works fine - but the problem is that that observer function is never triggered (unless I specifically set the data of the array as described above. Commented Nov 27, 2013 at 4:49
  • How is the model being updated? Are you using save()? Also, when the model saves, is your rails app involved via an API? Commented Nov 27, 2013 at 5:01
  • I'm basically polling my server for updates on my model using setInterval() to reload() my model. So the changes to the data do not happen on the front end, but is done by rails. Any other attribute changes (strings, floats) show up properly, but the observer for the array is not triggered for some reason. Commented Nov 27, 2013 at 8:54
  • Is your server responding with JSON? Are you sure that there are changes being made to the model on the server? Commented Nov 27, 2013 at 15:04

1 Answer 1

1

You might want to observe data.@each instead of data to monitor Arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.