Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I create custom ember-data methods?

I'm wondering if there's any way for me to create a custom ember-data method so I can do things like this:

this.store.customMethod('klass')

I know I can override the existing methods for my needs but I need more control than that

like image 834
ThreeAccents Avatar asked Feb 10 '23 08:02

ThreeAccents


2 Answers

Most likely what you want to do is add a custom methods to your models adapter if you want to do custom things. So in adapters/klass.js:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  customMethod(){
    const data = {user: {email: this.get('email')}};
    const baseUrl = this.urlForFindAll('user');
    return Ember.$.post(`${baseUrl}/password`, data);
  }
});

Then somewhere else:

const adapter = this.store.adapterFor('user');
adapter.customMethod();

But yeah sure you can extend the store. In stores/application.js:

import DS from 'ember-data';

export default DS.Store.extend({
    customMethod(model) {
      // Do stuff
    }
});
like image 60
Kit Sunde Avatar answered Feb 12 '23 00:02

Kit Sunde


Another option is to implement a custom store service.

// app/services/store.js
import DS from 'ember-data';

export default DS.Store.extend({

  init: function() {
    console.log('Using custom store!');
    return this._super.apply(this, arguments);
  }

});

https://stackoverflow.com/a/34999549/634545

like image 31
user634545 Avatar answered Feb 12 '23 00:02

user634545