My code is like this
// config const TEST_JSON = require('./test.json') const API_MAP = { testA: 'someroot' } const FAKE_API_MAP = { testA: TEST_JSON } // here model let BaseModel = Backbone.Model.extend({ url: function() { return `${HOST}${API_MAP[this.resourceName]}/` } }) let FakeModel = Backbone.Model.extend({ fetch: function(options) { return this.sync('', this, _.extend({}, options)); }, sync: function(method, model, options) { this.set(FAKE_API_MAP[this.resourceName], this.options) this.trigger('sync', this); }, }); // now it easy for switch them let modelA = new BaseModel({ resourceName: 'testA' }) modelA.fetch() let fakeModelA = new FakeModel({ resourceName: 'testA' }) fakeModelA.fetch()
source share