-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapters and Serializers are Store Managed
Adapters and Serializers are now store managed - with each store instance holding a unique instance of each serializer and adapter. They are no longer singletons. This allows multiple stores to be used with ember-data.
- Loading branch information
Showing
8 changed files
with
260 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
packages/ember-data/tests/integration/multiple_stores_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
var get = Ember.get; | ||
var HomePlanet, league, SuperVillain, EvilMinion, YellowMinion, DoomsdayDevice, MediocreVillain, env; | ||
var run = Ember.run; | ||
|
||
module("integration/multiple_stores - Multiple Stores Tests", { | ||
setup: function() { | ||
SuperVillain = DS.Model.extend({ | ||
firstName: DS.attr('string'), | ||
lastName: DS.attr('string'), | ||
homePlanet: DS.belongsTo("homePlanet", {inverse: 'villains'}), | ||
evilMinions: DS.hasMany("evilMinion") | ||
}); | ||
HomePlanet = DS.Model.extend({ | ||
name: DS.attr('string'), | ||
villains: DS.hasMany('superVillain', {inverse: 'homePlanet'}) | ||
}); | ||
EvilMinion = DS.Model.extend({ | ||
superVillain: DS.belongsTo('superVillain'), | ||
name: DS.attr('string') | ||
}); | ||
|
||
env = setupStore({ | ||
superVillain: SuperVillain, | ||
homePlanet: HomePlanet, | ||
evilMinion: EvilMinion | ||
}); | ||
|
||
env.store.modelFor('superVillain'); | ||
env.store.modelFor('homePlanet'); | ||
env.store.modelFor('evilMinion'); | ||
|
||
env.container.register('serializer:application', DS.ActiveModelSerializer); | ||
env.container.register('serializer:-active-model', DS.ActiveModelSerializer); | ||
env.container.register('adapter:-active-model', DS.ActiveModelAdapter); | ||
|
||
|
||
env.container.register('store:store-a', DS.Store); | ||
env.container.register('store:store-b', DS.Store); | ||
|
||
env.store_a = env.container.lookup('store:store-a'); | ||
env.store_b = env.container.lookup('store:store-b'); | ||
}, | ||
|
||
teardown: function() { | ||
run(env.store, 'destroy'); | ||
} | ||
}); | ||
|
||
test("should be able to push into multiple stores", function() { | ||
env.container.register('adapter:homePlanet', DS.ActiveModelAdapter); | ||
env.container.register('serializer:homePlanet', DS.ActiveModelSerializer); | ||
|
||
var home_planet_main = { id: '1', name: 'Earth' }; | ||
var home_planet_a = { id: '1', name: 'Mars' }; | ||
var home_planet_b = { id: '1', name: 'Saturn' }; | ||
|
||
run(env.store, 'push', 'homePlanet', home_planet_main); | ||
run(env.store_a, 'push', 'homePlanet', home_planet_a); | ||
run(env.store_b, 'push', 'homePlanet', home_planet_b); | ||
|
||
run(env.store, 'find', 'homePlanet', 1).then(async(function(homePlanet) { | ||
equal(homePlanet.get('name'), "Earth"); | ||
})); | ||
|
||
run(env.store_a, 'find', 'homePlanet', 1).then(async(function(homePlanet) { | ||
equal(homePlanet.get('name'), "Mars"); | ||
})); | ||
|
||
run(env.store_b, 'find', 'homePlanet', 1).then(async(function(homePlanet) { | ||
equal(homePlanet.get('name'), "Saturn"); | ||
})); | ||
|
||
}); | ||
|
||
test("embedded records should be created in multiple stores", function() { | ||
|
||
env.container.register('store:primary', DS.Store); | ||
env.container.register('store:secondary', DS.Store); | ||
|
||
env.primaryStore = env.container.lookup('store:primary'); | ||
env.secondaryStore = env.container.lookup('store:secondary'); | ||
|
||
env.container.register('adapter:superVillain', DS.ActiveModelAdapter); | ||
env.container.register('adapter:homePlanet', DS.ActiveModelAdapter); | ||
|
||
env.container.register('serializer:homePlanet', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, { | ||
attrs: { | ||
villains: {embedded: 'always'} | ||
} | ||
})); | ||
|
||
var serializer = env.store.serializerFor("homePlanet"); | ||
var serializer_primary = env.primaryStore.serializerFor("homePlanet"); | ||
var serializer_secondary = env.secondaryStore.serializerFor("homePlanet"); | ||
|
||
var json_hash = { | ||
home_planet: { | ||
id: "1", | ||
name: "Earth", | ||
villains: [{ | ||
id: "1", | ||
first_name: "Tom", | ||
last_name: "Dale" | ||
}] | ||
} | ||
}; | ||
var json_hash_primary = { | ||
home_planet: { | ||
id: "1", | ||
name: "Mars", | ||
villains: [{ | ||
id: "1", | ||
first_name: "James", | ||
last_name: "Murphy" | ||
}] | ||
} | ||
}; | ||
var json_hash_secondary = { | ||
home_planet: { | ||
id: "1", | ||
name: "Saturn", | ||
villains: [{ | ||
id: "1", | ||
first_name: "Jade", | ||
last_name: "John" | ||
}] | ||
} | ||
}; | ||
var json, json_primary, json_secondary; | ||
|
||
// debugger; | ||
|
||
run(function(){ | ||
json = serializer.extractSingle(env.store, HomePlanet, json_hash); | ||
equal(env.store.hasRecordForId("superVillain","1"), true, "superVillain should exist in store:main"); | ||
}); | ||
|
||
run(function(){ | ||
json_primary = serializer_primary.extractSingle(env.primaryStore, HomePlanet, json_hash_primary); | ||
equal(env.primaryStore.hasRecordForId("superVillain","1"), true, "superVillain should exist in store:primary"); | ||
}); | ||
|
||
run(function(){ | ||
json_secondary = serializer_secondary.extractSingle(env.secondaryStore, HomePlanet, json_hash_secondary); | ||
equal(env.secondaryStore.hasRecordForId("superVillain","1"), true, "superVillain should exist in store:secondary"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.