Javascript 连锁承诺和原型`
我很难得到承诺在原型中使用正确的Javascript 连锁承诺和原型`,javascript,angularjs,jasmine,karma-jasmine,angular-promise,Javascript,Angularjs,Jasmine,Karma Jasmine,Angular Promise,我很难得到承诺在原型中使用正确的范围 这是我的密码: 'use strict'; angular.module('testApp').factory('UrlSearchApi', function($resource, URL_SEARCH_API, PAGE_SIZE, $q){ var resource = $resource(URL_SEARCH_API); resource.Scroll = function () { return this.reset();
范围
这是我的密码:
'use strict';
angular.module('testApp').factory('UrlSearchApi',
function($resource, URL_SEARCH_API, PAGE_SIZE, $q){
var resource = $resource(URL_SEARCH_API);
resource.Scroll = function () {
return this.reset();
};
resource.Scroll.prototype.reset = function () {
this.visibleItems = [];
this.allItems = [];
this.busy = null;
return this;
};
resource.Scroll.prototype.fetch = function(query){
var params = {};
if(query) { params.q = query; }
return resource.query(params).$promise;
};
resource.Scroll.prototype.loadAllItems = function (results) {
var d = $q.defer();
angular.forEach(results, function (result, i) {
this.allItems.push(result);
if(i === results.length - 1 ) { d.resolve(); }
}, this);
return d.promise;
};
resource.Scroll.prototype.loadVisibleItems = function () {
var length = this.visibleItems.length,
offset = parseInt(length / PAGE_SIZE),
start = PAGE_SIZE * offset,
end = start + PAGE_SIZE,
subset = this.allItems.slice(start, end),
d = $q.defer();
angular.forEach(subset, function (item, i) {
this.visibleItems.push(item);
if(i === subset.length - 1 ) { d.resolve(); }
}, this);
return d.promise;
};
resource.Scroll.prototype.nextPage = function (query) {
if(this.busy) { return; }
console.log('nextPage ', query);
var tasks = [],
that = this;
if(!this.allItems.length) {
this.reset();
this.busy = true;
return this.fetch(query)
.then(this.loadAllItems)
.then(this.loadVisibleItems)
.finally(function () {
this.busy = false;
});
} else {
this.busy = true;
return this.loadVisibleItems().finally(function () {
this.busy = false;
});
}
};
return resource;
});
每当我运行测试时,我都会得到
describe('#nextPage', function () {
var scroll;
describe('when there is NO search term (show all)', function () {
beforeEach(function (done) {
scroll = new UrlSearchApi.Scroll();
$httpBackend.expectGET('/policy/search')
.respond(200, arrayGenerator(123));
scroll.nextPage().then(done);
$httpBackend.flush();
$rootScope.$apply();
});
it('should load all the items in all items variable', function () {
expect(scroll.allItems.length).toBe(123);
});
});
}))
我得到以下错误:
TypeError: 'undefined' is not an object (evaluating 'this.allItems')
现在,在严格模式下,$q
将内的设置为未定义。我试着在多个地方使用bind(this)
,但运气不好。。。有什么想法吗?我已经回答了这样一个问题。
如果您还有问题,请在评论中告诉我
Upd.尝试更新resource.Scroll.prototype.nextPage方法,如下所示:
if(!this.allItems.length) {
this.reset();
this.busy = true;
return this.fetch(query)
.then(this.loadAllItems.bind(this)) //bind here
.then(this.loadVisibleItems.bind(this)) // here
.finally(function () {
this.busy = false;
}.bind(this)); //and here
但请记住-当您将函数作为回调传递给时,然后
或传递给forEach
e.t.c它将丢失此
上下文。因此,当您将使用此
语法的函数作为回调函数传递时,请准确地使用bind
。如果您认为发现了重复的问题,请将其标记,而不是发布一个答案,该答案是另一个答案的副本/链接。