问题描述
我有一个非常奇怪的问题.我有一个Backbone集合,并且正在使用where方法在集合中查找与某个属性匹配的模型.我的问题是结果不一致.
我有一个joinGoalList,它跟踪用户已加入的目标.假设该集合包含ID为1和3的两个目标.当用户访问/goals/3时,将显示一条消息,说明用户已加入目标
我在访问/goals/3时遇到问题,该消息显示的时间是一半,而另一半则是不显示.
奇怪的是,此问题仅在我的远程服务器上发生,而不在我的本地主机上发生.
在我的代码中,我向joinedGoalList查询ID为3的ID,如果匹配,我知道matches数组必须大于0,所以我呈现出表明用户已加入目标的消息./p>
这是代码(joinedGoalList是Backbone集合:
console.log(joinedGoalList);
var matches = joinedGoalList.where({id: this.model.get("id")});
console.log(matches);
console.log(matches.length);
if (matches.length > 0) {
console.log("the matches length is > 0");
this.renderLeaveGoal();
} else {
console.log("the matches length is 0");
this.renderJoinGoal();
}
这是console.log(joinedGoalList)
的结果,这是结果(它们是一致的):
child
_byCid: Object
_byId: Object
_callbacks: Object
length: 2
models: Array[2]
__proto__: ctor
如您所见,长度为2.其中一个对象的ID为1,另一个对象的ID为3.这在整个页面加载过程中都是一致的.
当我对ID为3的对象在数组上进行匹配时,会发生不一致.某些页面加载会找到匹配项,而其他页面加载会找不到匹配项.
在远程服务器上,console.log(matches.length)
的结果为0或1,而在本地主机上,结果始终为1.
我很确定事件的顺序是这样的:
- 您在集合上调用
fetch
从服务器加载数据 - 您调用
console.log(joinedGoalList)
,这在某些浏览器中是异步的. - 您调用
joinedGoalList.where
并找到一个空集合. - 1 中的
fetch
调用返回并填充集合. - 2 中的
console.log
调用执行并打印出填充的集合,该调用将引用joinedGoalList
,并且该引用现在指向填充的集合.
在本地执行此操作时, 4 中的AJAX fetch
会很快返回,因此在 3 之前执行步骤 4 .您期望的方式.
您在这里有几个选择:
-
fetch
具有success
回调:因此,您可以使用
success
回调将调用where
的任何内容延迟到服务器响应并填充集合为止. -
fetch
重置集合:和
reset
将因此,您可以侦听
"reset"
事件并使用该事件触发调用where
的任何事件.
I am having an extremely bizarre problem. I have a Backbone collection, and I am using the where method to find models in the collection that match a certain attribute. My problem is the inconsistency of the results.
I have a joinedGoalList which keeps track of goals that a user has joined. Let's say that this collection contains two goals with IDs of 1 and 3. When a user accesses /goals/3, a message should display saying that the user has joined the goal
I am having a problem where I am accessing /goals/3, and half the time the message displays, and the other half of the time, the message does not display.
The odd thing is that this problem only happens on my remote server and not on my local host.
In my code, I query the joinedGoalList for an ID of 3, and if it matches, I know that the matches array has to be greater than 0, so I render the message showing that the user has joined the goal.
Here is the code (joinedGoalList is a Backbone collection:
console.log(joinedGoalList);
var matches = joinedGoalList.where({id: this.model.get("id")});
console.log(matches);
console.log(matches.length);
if (matches.length > 0) {
console.log("the matches length is > 0");
this.renderLeaveGoal();
} else {
console.log("the matches length is 0");
this.renderJoinGoal();
}
Here are the results of console.log(joinedGoalList)
, here are the results(they are consistent):
child
_byCid: Object
_byId: Object
_callbacks: Object
length: 2
models: Array[2]
__proto__: ctor
As you can see, the length is 2. One of the objects has an ID of 1 and the other object has an ID of 3. This is consistent throughout the page loads.
The inconsistency occurs when I do a match on the array for an object with an ID of 3. Some page loads find the match while other page loads do not find the match.
The results of console.log(matches.length)
are either 0 or 1 on my remote server, yet on my localhost, the results are always 1.
I'm pretty sure that the sequence of events goes like this:
- You call
fetch
on the collection to load your data from the server. - You call
console.log(joinedGoalList)
, this is asynchronous in some browsers. - You call
joinedGoalList.where
and find an empty collection. - The
fetch
call from 1 returns and populates the collection. - The
console.log
call from 2 executes and prints out the populated collection, this call will have a reference tojoinedGoalList
and that reference will now be pointing at a populated collection.
When you do this locally, the AJAX fetch
in 4 returns quite quickly so step 4 occurs before 3 and everything behaves the way you'e expecting it to.
You have a couple options here:
fetch
has asuccess
callback:So you could use the
success
callback to delay whatever is callingwhere
until the server has responded and the collection is populated.fetch
resets the collection:and
reset
willSo you could listen for the
"reset"
event and use that event to trigger whatever is callingwhere
.
这篇关于数组有时显示长度为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!