Permalink
Browse files

More advanced FixtureAdapter implementation

  • Loading branch information...
tchak committed Jun 6, 2012
1 parent 0047d67 commit 0e81ecfc071a9e1703ad7ad3de00aca66265b4b7
Showing with 214 additions and 26 deletions.
  1. +111 −13 packages/ember-data/lib/adapters/fixture_adapter.js
  2. +103 −13 packages/ember-data/tests/unit/fixture_adapter_test.js
@@ -1,30 +1,128 @@
require("ember-data/core");
require("ember-data/system/adapters");
DS.fixtureAdapter = DS.Adapter.create({
var get = Ember.get;
DS.FixtureAdapter = DS.Adapter.extend({
simulateRemoteResponse: true,
latency: 50,
/*
Implement this method in order to provide data associated with a type
*/
fixturesForType: function(type) {
return type.FIXTURES ? Ember.A(type.FIXTURES) : null;
},
/*
Implement this method in order to query fixtures data
*/
queryFixtures: function(fixtures, query) {
return fixtures;
},
/*
Implement this method in order to provide provide json for CRUD methods
*/
mockJSON: function(type, record) {
return record.toJSON({associations: true});
},
/*
Adapter methods
*/
generateIdForRecord: function(store, record) {
return Ember.guidFor(record);
},
find: function(store, type, id) {
var fixtures = type.FIXTURES;
var fixtures = this.fixturesForType(type);
if (fixtures) {
fixtures = fixtures.findProperty('id', id);
}
Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);
if (fixtures.hasLoaded) { return; }
setTimeout(function() {
store.loadMany(type, fixtures);
fixtures.hasLoaded = true;
}, 300);
this.simulateRemoteCall(function() {
store.load(type, fixtures);
}, store, type);
},
findMany: function() {
this.find.apply(this, arguments);
findMany: function(store, type, ids) {
var fixtures = this.fixturesForType(type);
if (fixtures) {
fixtures = fixtures.filter(function(item) {
return ids.indexOf(item.id) !== -1;
});
}
Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);
this.simulateRemoteCall(function() {
store.loadMany(type, fixtures);
}, store, type);
},
findAll: function(store, type) {
var fixtures = type.FIXTURES;
var fixtures = this.fixturesForType(type);
Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);
var ids = fixtures.map(function(item, index, self){ return item.id; });
store.loadMany(type, ids, fixtures);
}
this.simulateRemoteCall(function() {
store.loadMany(type, fixtures);
}, store, type);
},
findQuery: function(store, type, query, array) {
var fixtures = this.fixturesForType(type);
fixtures = this.queryFixtures(fixtures, query);
Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);
this.simulateRemoteCall(function() {
array.load(fixtures);
}, store, type);
},
createRecord: function(store, type, record) {
var fixture = this.mockJSON(type, record);
fixture.id = this.generateIdForRecord(store, record);
this.simulateRemoteCall(function() {
store.didCreateRecord(record, fixture);
}, store, type, record);
},
updateRecord: function(store, type, record) {
var fixture = this.mockJSON(type, record);
this.simulateRemoteCall(function() {
store.didUpdateRecord(record, fixture);
}, store, type, record);
},
deleteRecord: function(store, type, record) {
this.simulateRemoteCall(function() {
store.didDeleteRecord(record);
}, store, type, record);
},
/*
@private
*/
simulateRemoteCall: function(callback, store, type, record) {
if (get(this, 'simulateRemoteResponse')) {
setTimeout(callback, get(this, 'latency'));
} else {
callback();
}
}
});
DS.fixtureAdapter = DS.FixtureAdapter.create();
@@ -1,19 +1,29 @@
var get = Ember.get, set = Ember.set;
var store, Person;
module("DS.FixtureAdapter");
module("DS.FixtureAdapter", {
setup: function() {
store = DS.Store.create({
adapter: 'DS.fixtureAdapter'
});
test("should load all data for a type asynchronously the first time it is requested", function() {
var store = DS.Store.create({
adapter: 'DS.fixtureAdapter'
});
var Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
height: DS.attr('number')
});
height: DS.attr('number')
});
},
teardown: function() {
Ember.run(function() {
store.destroy();
});
store = null;
Person = null;
}
});
test("should load data for a type asynchronously when it is requested", function() {
Person.FIXTURES = [{
id: 'wycats',
firstName: "Yehuda",
@@ -42,10 +52,90 @@ test("should load all data for a type asynchronously the first time it is reques
equal(get(ebryn, 'height'), 70, "data from fixtures is loaded correctly");
var wycats = store.find(Person, 'wycats');
equal(get(wycats, 'isLoaded'), true, "subsequent requests for records are returned immediately");
equal(get(wycats, 'height'), 65, "subsequent requested records contain correct information");
wycats.addObserver('isLoaded', function() {
clearTimeout(timer);
start();
equal(get(wycats, 'isLoaded'), true, "subsequent requests for records are returned asynchronously");
equal(get(wycats, 'height'), 65, "subsequent requested records contain correct information");
});
stop();
timer = setTimeout(function() {
start();
ok(false, "timeout exceeded waiting for fixture data");
}, 1000);
});
stop();
var timer = setTimeout(function() {
start();
ok(false, "timeout exceeded waiting for fixture data");
}, 1000);
});
test("should create record asynchronously when it is commited", function() {
var paul = Person.createRecord({firstName: 'Paul', lastName: 'Chavard', height: 70});
paul.on('didCreate', function() {
clearTimeout(timer);
start();
equal(get(paul, 'isNew'), false, "data loads asynchronously");
equal(get(paul, 'isDirty'), false, "data loads asynchronously");
equal(get(paul, 'height'), 70, "data from fixtures is saved correctly");
});
store.commit();
stop();
var timer = setTimeout(function() {
start();
ok(false, "timeout exceeded waiting for fixture data");
}, 1000);
});
test("should update record asynchronously when it is commited", function() {
var paul = store.findByClientId(Person, store.load(Person, 1, {first_name: 'Paul', last_name: 'Chavard', height: 70}).clientId);
paul.set('height', 80);
paul.on('didUpdate', function() {
clearTimeout(timer);
start();
equal(get(paul, 'isDirty'), false, "data loads asynchronously");
equal(get(paul, 'height'), 80, "data from fixtures is saved correctly");
});
store.commit();
stop();
var timer = setTimeout(function() {
start();
ok(false, "timeout exceeded waiting for fixture data");
}, 1000);
});
test("should delete record asynchronously when it is commited", function() {
var paul = store.findByClientId(Person, store.load(Person, 1, {first_name: 'Paul', last_name: 'Chavard', height: 70}).clientId);
paul.deleteRecord();
paul.on('didDelete', function() {
clearTimeout(timer);
start();
equal(get(paul, 'isDeleted'), true, "data deleted asynchronously");
equal(get(paul, 'isDirty'), false, "data deleted asynchronously");
});
store.commit();
stop();
var timer = setTimeout(function() {

0 comments on commit 0e81ecf

Please sign in to comment.