Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'master' of github.com:SitePen/dstore
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Feb 11, 2014
2 parents e46cd6b + 20c6d59 commit c234627
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
12 changes: 8 additions & 4 deletions Observable.js
Expand Up @@ -94,7 +94,11 @@ define([
);
}

var observed = this._createSubCollection({
// delegate rather than call _createSubCollection because we are not ultimately creating
// a new collection, just decorating an existing collection with item index tracking.
// If we use _createSubCollection, it will return a new collection that may exclude
// important, defining properties from the tracked collection.
var observed = lang.delegate(this, {
// Any sub-collections created from the tracked collection should be based on this
// parent collection instead
_createSubCollection: lang.hitch(this, '_createSubCollection'),
Expand All @@ -121,7 +125,7 @@ define([
};

var ranges = [];
if (this.hasOwnProperty('data')) {
if (this.data) {
observed.data = this.data.slice(0); // make local copy
// Treat in-memory data as one range to allow a single code path for all stores
registerRange(ranges, 0, observed.data.length);
Expand Down Expand Up @@ -245,7 +249,7 @@ define([
} else {
// we don't have a queryEngine, so we can't provide any information
// about where it was inserted or moved to. If it is an update, we leave
// its position alone. other we at least indicate a new object
// its position alone. otherwise, we at least indicate a new object

if (type === 'update') {
insertedInto = removedFrom;
Expand All @@ -257,7 +261,7 @@ define([

for (i = 0; insertionRangeIndex === -1 && i < ranges.length; ++i) {
range = ranges[i];
if (range.start <= insertedInto && insertedInto < (range.start + range.count)) {
if (range.start <= insertedInto && insertedInto <= (range.start + range.count)) {
insertionRangeIndex = i;
}
}
Expand Down
47 changes: 45 additions & 2 deletions tests/Observable.js
Expand Up @@ -376,8 +376,7 @@ define([
headTrimmingRange = { start: 50, end: 60 },
tailTrimmingRange = { start: 90, end: 100 };

var observations = [],
trackedStore = store.track(),
var trackedStore = store.track(),
assertRangeDefined = function (start, end) {
for(var i = start; i < end; ++i) {
assert.notEqual(trackedStore.partialData[i], undefined);
Expand Down Expand Up @@ -449,6 +448,50 @@ define([
});
},

'new item in empty store - with queryExecutor': function () {
var store = new MyStore({ data: [] }),
collection = store.filter({ type: 'test-item' }).track();

var actualEvent;
collection.on('add', function (event) {
actualEvent = event;
});

var expectedTarget = collection.add({
type: 'test-item',
id: 1,
name: 'one'
});

assert.deepEqual(actualEvent, {
type: 'add',
index: 0,
target: expectedTarget
});
},

'new item in empty store - without queryExecutor': function () {
var store = new MyStore({ data: [] }),
collection = store.track();

var actualEvent;
collection.on('add', function (event) {
actualEvent = event;
});

var expectedTarget = collection.add({
type: 'test-item',
id: 1,
name: 'one'
});

assert.deepEqual(actualEvent, {
type: 'add',
index: 0,
target: expectedTarget
});
},

'type': function () {
assert.isFalse(store === store.track(function () {}));
},
Expand Down

0 comments on commit c234627

Please sign in to comment.