Skip to content

Commit c686347

Browse files
author
Ed Hinchliffe
committed
fix: Fixed $localForage.bind with falsey defaults (+ test).
1 parent cca7fdc commit c686347

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/angular-localForage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@
365365
model = $parse(scopeKey);
366366

367367
return self.getItem(opts.key).then(function(item) {
368-
if(item) { // If it does exist assign it to the $scope value
368+
if (item !== null) { // If it does exist assign it to the $scope value
369369
model.assign($scope, item);
370-
} else if(opts.defaultValue) { // If a value doesn't already exist store it as is
370+
} else if(!angular.isUndefined(opts.defaultValue)) { // If a value doesn't already exist store it as is
371371
model.assign($scope, opts.defaultValue);
372372
self.setItem(opts.key, opts.defaultValue);
373373
}

tests/angular-localForage.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ describe('Module: LocalForageModule', function() {
6666

6767
$localForage.getItem('this key is unknown').then(function(value) {
6868
stopDigests(interval);
69-
expect(value).toBeNull()
69+
expect(value).toBeNull();
7070
done();
7171
}, done);
7272
});
73-
73+
7474
it('setItem and getItem should work', function(done) {
7575
var interval = triggerDigests();
7676

@@ -85,7 +85,7 @@ describe('Module: LocalForageModule', function() {
8585

8686
}, done);
8787
});
88-
88+
8989
it('setItem and getItem should work with an array of keys', function(done) {
9090
var interval = triggerDigests(),
9191
values = ['Olivier Combe', 'AngularJs', 'Open Source'];
@@ -326,6 +326,40 @@ describe('Module: LocalForageModule', function() {
326326
$localForage.setItem();
327327
}).toThrowError('You must define a key to set');
328328
});
329+
330+
describe("bind", function() {
331+
var $scope, $q;
332+
333+
beforeEach(inject(function($rootScope, _$q_){
334+
$scope = $rootScope;
335+
$q = _$q_;
336+
}));
337+
338+
it(' should use the default stored value if nothing has been previously stored', function(done){
339+
// Check different types of items.
340+
var testItems = [ { foo: 'bar' }, ["cat", "dog", "pidgeon"], 123, 0, true, false ]
341+
var promises = [];
342+
// Store all the items, deleting old values
343+
for(var i = 0; i < testItems.length; i++){
344+
$localForage.removeItem('item' + i);
345+
var item = testItems[i];
346+
promises.push(
347+
$localForage.bind($scope, {
348+
key: 'item' + i,
349+
defaultValue: item,
350+
})
351+
);
352+
}
353+
// After all promises have been resolved, check the items are what we expect them to be.
354+
$q.all(promises).then(function(){
355+
for(var i = 0; i < testItems.length; i++){
356+
expect($scope['item' + i]).toBe(testItems[i]);
357+
}
358+
});
359+
done();
360+
});
361+
362+
});
329363

330364
describe("iterate", function() {
331365
var interval;

0 commit comments

Comments
 (0)