Silvio Langereis
Silvio Langereis

Reputation: 471

createRecord with custom ID in emberjs 2.x and Firebase 3.x

Until now, I saved all my data with the following line of code:

saveUser(params) {
      let newUser = this.store.createRecord('user', params);
      newUser.save();
      this.transitionTo('index');

This worked fine, but didn't allow for custom ID's in firebase, so I changed it into:

saveUser(params) {
      let newUser = this.store.createRecord('user', {
        id: params.loginId,
        name: params.name,
        nickname: params.nickname,
        imageUrl: params.imageUrl,
        email: params.email
      });
      newUser.save();
      this.transitionTo('index');

Processes them exactly as I want them to be stored on the Firebase database, so no problem there. I'm wondering though, and not finding any solution on the web, how I can combine the two, so that I don't have to bind every param. It's bound to give problems when I add/remove model properties. Something I'm looking for would look like this (pseudo, yes I tried it, didn't work!):

let newUser = this.store.createRecord('user', {id: params.loginId}, params);

In short, I'm looking for the dynamic properties of ('model', params), but with the option to manually adjust 1 (or more) records without having to type out all of the params.

Thanks in advance !

Upvotes: 2

Views: 324

Answers (1)

IAMZERG
IAMZERG

Reputation: 100

You will probably want to customize your serializer to accomplish this. The example in the docs is a good one, so it should be pretty straightforward: https://guides.emberjs.com/v2.13.0/models/customizing-serializers/

I am, of course, assuming you are using Ember Data for your models.

Upvotes: 1

Related Questions