2

I've been working on implementing tagging with rails and ember, based on this railscast: http://railscasts.com/episodes/382-tagging

I've setup an ember-data model with a tagList property, which I'd like to set every time a tag changes (using the following setTagList method):

App.Post = DS.Model.extend({
  tags: DS.hasMany('App.Tag'),
  tagList: DS.attr('string'),

  setTagList: function() {
    if(this.get('tags.length')) {
      var tagList = this.get('tags').map(function(tag) {
        return tag.get('name');
      }).join(', ');
      this.set('tagList', tagList);
    }
  }.observes('isLoaded', 'tags', 'tags.@each.name')
});

The first error I ran into was:

Uncaught TypeError: Cannot call method 'send' of null

Which I believe is as a result of observing tags and tags.@each.name. I've also read that there may be issues with observing nested properties.

Secondly, by removing those dependencies to leave .observes('isLoaded'), the following error is thrown:

Uncaught Error: Attempted to handle event `materializingData` on <App.Post:ember283:1> while in state rootState. Called with undefined

Which is caused by any call to this.get('tags').

See the JSBin: http://jsbin.com/iyoyax/7/edit

Can anyone advise how I might observe an associated model to set another attribute on the same model?

Thanks in advance

Dom Christie
  • 4,152
  • 3
  • 23
  • 31
  • I see some unusual things here. Why do you have a `setTagList` observer? Should it be setting the `tagList` property (it's not doing that currently)? And if so, why not make `tagList` a computed property? – mehulkar Jul 22 '13 at 23:51
  • I've updated the example to include setting the tagList property. It cannot be a computed property because it is persisted (and therefore needs to be a `DS.attr`) – Dom Christie Jul 24 '13 at 20:28

1 Answers1

0

I'm not sure why your case isn't specifically working, but I'd be willing to bet that using ember-data-latest with ember rc2 has something to do with it.

Your code seems fine. Here's an fiddle that is working: http://jsfiddle.net/DBtu6/5/

The only changes I made were to use rc6 with ember-data-latest and to change your map function to mapProperty.

I also used JSFiddle instead of JSBin because JSBin because I couldn't figure out how to use the latter's console and I was just more familiar with fiddle. Hope this helps.

mehulkar
  • 4,895
  • 5
  • 35
  • 55