Permalink
Browse files

Initial commit of the BasicAdapter

  • Loading branch information...
tomhuda
tomhuda committed Mar 7, 2013
1 parent a6addab commit 25b78d0f1e3bbdc8e144e618812e4c5c3bf342f2
View
@@ -1,4 +1,5 @@
-instance_eval File.read(EmberDev.support_path.join('Assetfile'))
+require 'ember-dev'
+instance_eval File.read(::EmberDev.support_path.join('Assetfile'))
distros = {
:full => %w(ember-data)
@@ -1,2 +1,3 @@
require("ember-data/adapters/fixture_adapter");
require("ember-data/adapters/rest_adapter");
+require("ember-data/adapters/basic_adapter");
@@ -0,0 +1,29 @@
+var ObjectProcessor = function(json, type, store) {
+ this.json = json;
+ this.type = type;
+ this.store = store;
+};
+
+ObjectProcessor.prototype = {
+ load: function() {
+ this.store.load(this.type, this.json);
+ }
+};
+
+var processorFactory = function(store, type) {
+ return function(json) {
+ return new ObjectProcessor(json, type, store);
+ };
+};
+
+
+DS.BasicAdapter = DS.Adapter.extend({
+ find: function(store, type, id) {
+ var sync = type.sync;
+
+ Ember.assert("You are trying to use the BasicAdapter to find id '" + id + "' of " + type + " but " + type + ".sync was not found", sync);
+ Ember.assert("The sync code on " + type + " does not implement find(), but you are trying to find id '" + id + "'.", sync.find);
+
+ sync.find(id, processorFactory(store, type));
+ }
+});
@@ -0,0 +1,45 @@
+var get = Ember.get;
+
+var store, adapter, Person;
+module("Basic Adapter", {
+ setup: function() {
+ adapter = DS.BasicAdapter.create();
+ store = DS.Store.create({
+ adapter: adapter
+ });
+
+ var attr = DS.attr;
+ Person = DS.Model.extend({
+ firstName: attr('string'),
+ lastName: attr('string')
+ });
+ },
+
+ teardown: function() {
+ Ember.run(function() {
+ store.destroy();
+ adapter.destroy();
+ });
+ }
+});
+
+test("The sync object is consulted to load data", function() {
+ Person.sync = {
+ find: function(id, process) {
+ equal(id, "1", "The correct ID is passed through");
+ setTimeout(async(function() {
+ process({ id: 1, firstName: "Tom", lastName: "Dale" }).load();
+ }));
+ }
+ };
+
+ var person = Person.find(1);
+
+ equal(get(person, 'id'), "1", "The id is the coerced ID passed to find");
+
+ person.on('didLoad', async(function() {
+ equal(get(person, 'firstName'), "Tom");
+ equal(get(person, 'lastName'), "Dale");
+ equal(get(person, 'id'), "1", "The id is still the same");
+ }));
+});

0 comments on commit 25b78d0

Please sign in to comment.