2
我有一些奇怪的行爲與骨幹模型回事 - 屬性,我說我叫save()
和服務器響應被推出時間之間設置,更換由服務器響應(這只是響應保存的屬性)。Backbone.js的慢服務器保存()響應將覆蓋本地更改屬性
Backbone應該這樣表現嗎?當服務器延遲一點時,它會讓我的應用看起來非常瘋狂 - 它會覆蓋我的currentScreen
屬性,而舊屬性會向後導航屏幕。這裏是我的代碼:
Interview = Backbone.Model.extend({
urlRoot: "/interviews",
defaults: {
"currentScreen": 0
}
});
,這裏是我的茉莉花/ Sinon.JS測試描述我所期望的(它失敗Expected 0 to equal 1.
):
describe("Interview model", function() {
beforeEach(function() {
this.interview = new Interview;
});
describe("saving with a slow server response", function() {
beforeEach(function() {
this.server = sinon.fakeServer.create();
});
afterEach(function() {
this.server.restore();
});
it('should not overwrite locally changed attributes on server response', function() {
this.server.respondWith("PUT", "/interviews/1",
[200, { "Content-Type": "application/json" },
'{"id":1,"currentScreen":0}']);
this.interview.set({id: 1, currentScreen: 0});
this.interview.save();
this.interview.set({currentScreen: 1}); // user navigates to next screen
this.server.respond(); // return fake response after currentScreen changed locally
expect(this.interview.get('currentScreen')).toEqual(1);
});
});
});
謝謝!我的錯誤是使用'render:json => @ interview'(Rails後端)返回,這明顯返回了所有的模型屬性。我很榮幸Backbone的創建者回答了我的問題。 :-) –