Skip to content

Commit

Permalink
Deprecate store:main and store:application and recommend users always
Browse files Browse the repository at this point in the history
use service:store`app/services/store.js` to prevent duplicate stores.
  • Loading branch information
bmac committed Jun 13, 2015
1 parent bfda2ea commit ac93315
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/ember-data/lib/ember-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var K = Ember.K;
This code initializes Ember-Data onto an Ember application.
If an Ember.js developer defines a subclass of DS.Store on their application,
as `App.ApplicationStore` (or via a module system that resolves to `store:application`)
as `App.StoreService` (or via a module system that resolves to `service:store`)
this code will automatically instantiate it and make it available on the
router.
Expand All @@ -22,7 +22,7 @@ var K = Ember.K;
For example, imagine an Ember.js application with the following classes:
App.ApplicationStore = DS.Store.extend({
App.StoreService = DS.Store.extend({
adapter: 'custom'
});
Expand Down
6 changes: 3 additions & 3 deletions packages/ember-data/lib/initializers/store-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@param {Ember.Registry} registry
*/
export default function initializeStoreInjections(registry) {
registry.injection('controller', 'store', 'store:main');
registry.injection('route', 'store', 'store:main');
registry.injection('data-adapter', 'store', 'store:main');
registry.injection('controller', 'store', 'service:store');
registry.injection('route', 'store', 'service:store');
registry.injection('data-adapter', 'store', 'service:store');
}
5 changes: 4 additions & 1 deletion packages/ember-data/lib/initializers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export default function initializeStore(registry, application) {
registry.optionsForType('serializer', { singleton: false });
registry.optionsForType('adapter', { singleton: false });

// This will get deprecated later in the instace
// initializer. However we register it here so we have access to
// application.Store in the instance initializer.
if (application && application.Store) {
registry.register('store:application', application.Store);
registry.register('store:main', application.Store);
}

// allow older names to be looked up
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Store from "ember-data/system/store";
import ContainerProxy from "ember-data/system/container-proxy";

/**
Configures a registry for use with an Ember-Data
store.
Expand All @@ -25,14 +27,32 @@ export default function initializeStoreService(applicationOrRegistry) {
container = registry;
}
}

// Eagerly generate the store so defaultStore is populated.
var store;
if (registry.has('store:main')) {
Ember.deprecate('Registering a custom store as `store:main` or defining a store in app/store.js has been deprecated. Please move you store to `service:store` or define your custom store in `app/services/store.js`');
store = container.lookup('store:main');
} else {
var storeMainProxy = new ContainerProxy(registry);
storeMainProxy.registerDeprecations([
{ deprecated: 'store:main', valid: 'service:store' }
]);
}

if (registry.has('store:application')) {
var customStoreFactory = container.lookupFactory('store:application');
registry.register('store:main', customStoreFactory);
Ember.deprecate('Registering a custom store as `store:application` or defining a store in app/stores/application.js has been deprecated. Please move you store to `service:store` or define your custom store in `app/services/store.js`');
store = container.lookup('store:application');
} else {
registry.register('store:main', Store);
var storeApplicationProxy = new ContainerProxy(registry);
storeApplicationProxy.registerDeprecations([
{ deprecated: 'store:application', valid: 'service:store' }
]);
}

// Eagerly generate the store so defaultStore is populated.
var store = container.lookup('store:main');
registry.register('service:store', store, { instantiate: false });
if (store) {
registry.register('service:store', store, { instantiate: false });
} else {
registry.register('service:store', Store);
}
}
4 changes: 2 additions & 2 deletions packages/ember-data/tests/integration/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var app, App, container;
*/

function getStore() {
return lookup('store:main');
return lookup('service:store');
}

function lookup(thing) {
Expand Down Expand Up @@ -69,7 +69,7 @@ test("registering App.Store is deprecated but functional", function() {
'has been deprecated. Please use `App.ApplicationStore` instead.');

run(function() {
ok(lookup('store:main').get('isCustomButDeprecated'), "the custom store was instantiated");
ok(lookup('service:store').get('isCustomButDeprecated'), "the custom store was instantiated");
});

var fooController = lookup('controller:foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module('integration/backwards-compat/non-dasherized-lookups - non dasherized loo
name: DS.attr()
});
});
store = App.__container__.lookup('store:main');
store = App.__container__.lookup('service:store');
},
teardown: function() {
run(App, 'destroy');
Expand Down Expand Up @@ -67,7 +67,7 @@ module('integration/backwards-compat/non-dasherized-lookups - non dasherized loo
postNotes: DS.hasMany('post_note')
});
});
store = App.__container__.lookup('store:main');
store = App.__container__.lookup('service:store');
},

teardown: function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module("DS.DebugAdapter", {

});

store = App.__container__.lookup('store:main');
store = App.__container__.lookup('service:store');
debugAdapter = App.__container__.lookup('data-adapter:main');

debugAdapter.reopen({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test("embedded records should be created in multiple stores", function() {

run(function() {
json_main = serializer_main.extractSingle(env.store, env.store.modelFor('home-planet'), json_hash_main);
equal(env.store.hasRecordForId('super-villain', "1"), true, "superVillain should exist in store:main");
equal(env.store.hasRecordForId('super-villain', "1"), true, "superVillain should exist in service:store");
});

run(function() {
Expand Down
22 changes: 21 additions & 1 deletion packages/ember-data/tests/integration/setup-container-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module("integration/setup-container - Setting up a container", {
});

test("The store should be registered into a container.", function() {
ok(container.lookup('store:main') instanceof Store, "the custom store is instantiated");
ok(container.lookup('service:store') instanceof Store, "the custom store is instantiated");
});

test("The store should be registered into the container as a service.", function() {
Expand Down Expand Up @@ -86,6 +86,26 @@ test("serializers are not returned as singletons - each lookup should return a d
notEqual(serializer1, serializer2);
});

test("the deprecated store:main is resolved as service:store", function() {
var deprecated;
var valid = container.lookup('service:store');
expectDeprecation(function() {
deprecated = container.lookup('store:main');
});

ok(deprecated.constructor === valid.constructor, "they should resolve to the same thing");
});

test("the deprecated store:application is resolved as service:store", function() {
var deprecated;
var valid = container.lookup('service:store');
expectDeprecation(function() {
deprecated = container.lookup('store:application');
});

ok(deprecated.constructor === valid.constructor, "they should resolve to the same thing");
});

test("adapters are not returned as singletons - each lookup should return a different instance", function() {
var adapter1, adapter2;
adapter1 = container.lookup('adapter:-rest');
Expand Down

0 comments on commit ac93315

Please sign in to comment.