Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

moved the DS.RecordArray creation of Store#all(type) into a separate function #464

Closed
wants to merge 2 commits into from

3 participants

@dagda1

Previously Store#all had the creation of the returned DS.RecordArray hard coded into the function:

var array = DS.RecordArray.create({ type: type, content: Ember.A([]), store: this, isLoaded: true });
this.registerRecordArray(array, type);

This pull request gives the user an opportunity to supply a derived type:

    //Store#all
    var array = this.newRecordArray(type);

    this.registerRecordArray(array, type);
  newRecordArray: function(type){
    return DS.RecordArray.create({ type: type, content: Ember.A([]), store: this, isLoaded: true });
  },

For example, I might want to supply a PagedRecordArray that provides paging functionality.

@tomdale
Owner

I think this would more idiomatically be named createRecordArray instead of newRecordArray.

dagda1 added some commits
@igorT
Owner

Seems good, but could you elaborate on what the use case for this is?

@dagda1

It is an old PR and I do not think it is relevant anymore.

@dagda1 dagda1 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 20, 2012
  1. @dagda1

    moved the DS.RecordArray creation of Store#all(type) into a separate

    dagda1 authored
    function to allow the user to override and supply a derived
    DS.RecordArray type.
  2. @dagda1
This page is out of date. Refresh to see the latest.
View
13 packages/ember-data/lib/system/store.js
@@ -722,7 +722,9 @@ DS.Store = Ember.Object.extend(DS.Mappable, {
if (findAllCache) { return findAllCache; }
- var array = DS.RecordArray.create({ type: type, content: Ember.A([]), store: this, isLoaded: true });
+ var array = this.createRecordArray(type);
+ Ember.assert("You can only register an instance or subclass of DS.RecordArray", array instanceof DS.RecordArray);
+
this.registerRecordArray(array, type);
typeMap.findAllCache = array;
@@ -730,6 +732,15 @@ DS.Store = Ember.Object.extend(DS.Mappable, {
},
/**
+ * Override to supply a subclass of DS.RecordArray
+ @param {Class} type
+ @return {DS.RecordArray}
+ **/
+ createRecordArray: function(type){
+ return DS.RecordArray.create({ type: type, content: Ember.A([]), store: this, isLoaded: true });
+ },
+
+ /**
Takes a type and filter function, and returns a live RecordArray that
remains up to date as new records are loaded into the store or created
locally.
View
19 packages/ember-data/tests/unit/store_test.js
@@ -392,6 +392,25 @@ test("all(type) returns a record array of all records of a specific type", funct
strictEqual(results, store.all(Person), "subsequent calls to all return the same recordArray)");
});
+test("all(type) can return a subclass of RecordArray", function(){
+ var DerivedRecordArray = DS.RecordArray.extend();
+
+ var store = DS.Store.create({
+ adapter: DS.Adapter.create(),
+ createRecordArray: function(type){
+ return DerivedRecordArray.create({type: type, content: Ember.A([]), store: this});
+ }
+ });
+
+ var Person = DS.Model.extend({
+ name: DS.attr('string')
+ });
+
+ var array = store.all(Person);
+
+ equal(array.constructor, DerivedRecordArray, "Subclass of RecordArray returned from all");
+});
+
test("a new record of a particular type is created via store.createRecord(type)", function() {
var store = DS.Store.create();
var Person = DS.Model.extend({
Something went wrong with that request. Please try again.