rlivsey/fireplace

Setting a property on a relationship with a meta model does not persist

Closed this issue · 5 comments

I have a couple models:

var UserModel = FP.Model.extend({
  lists: FP.hasMany('list', { embedded: false, as: 'user-meta' }),
  folders: FP.hasMany('folder', { embedded: false, as: 'user-meta' })
});

var ListModel = FP.Model.extend({
  name: FP.attr('string')
});

var UserMetaModel = FP.MetaModel.extend({

});

If I get a reference to the list from an instance of a user, changing the name property and saving it will not persist to firebase. If I remove the as definition of the meta model it works as expected.

var list = user.get('lists').objectAt(0);
list.set('name', 'new name');
list.save(); // Does not persist to firebase

Is this working as intended? Is there another way to have this data persist in the save?

Now the reason, I am defining a meta model is because I wanted to define a priority on the foreign keys in the user model so that I can re-order the lists. Is there a way to do this without setting a meta model?

I was reading through the source and I discovered that this works:

var list = user.get('lists').objectAt(0);
list.set('name', 'new name');
list.get('content').save();

Is this the intended behavior?

I think this is related to this section of the documentation:

Keep in mind that, when using MetaModels, you have to set the actual model as the content property on the MetaModel and then add the MetaModel to the parent's collection like in this sample:

// project and person (of class People) are loaded already
var member = store.createRecord('member', {
  accessLevel: "admin",
  joinedAt: new Date(),
  content: person // the actual "content" of this relationship
});

project.get('members').addObject(member);
project.save();

So, yes, I think you have to save the actual model stored in content, as you discovered. When you get the objectAt(0), you're getting the MetaModel-wrapped list. Setting the name gets passed through, but it looks like you cannot save a MetaModel directly. Perhaps it would be better for save to proxy in the same way. Right now, a direct save is going to (I think) just deal with persisting the meta properties in your User model, though that may actually require a save on he User model itself.

Ahhh that makes sense now. The interesting thing is that I have never actually created a meta model. I only ever create instances of the other models and then call addObject but everything still works.

Do you know if it is possible to set a priority on the foreign key without using a meta model? I'm not actually storing any information on the meta model. I only defined it so that I could set a priority.

I don't believe so, if you mean a priority in how the index is stored in the User model. A priority has to have a named attribute to act on, and the only way I know of to add named attributes to indexes is through meta-models, by definition.

Yea so the data structure will be like:

users: {
  firebaseId: {
    lists: {
      firebaseListId1: true,
      firebaseListId2: true
    }
  }
}

I want to set a priority on firebaseListId1 and firebaseListId2 so that I can re-order them.