将变量从Backbone.js router initialize函数传递到另一个路由器函数的forEach语句
请参阅下面的更新代码… 我试图使用forEach语句按另一个集合的属性筛选一个集合。如何编写此代码以在forEach语句中维护对变量“this.neighborhoodiscollection”的引用。这可能不是一个问题,而是一个问题。我想,换句话说,我该如何将变量“this.neighborhoodiscollection”传递到我的forEach语句中。以下是我尝试采取的步骤: 我将获取我的故事集和邻居集的结果传递给路由器:将变量从Backbone.js router initialize函数传递到另一个路由器函数的forEach语句,backbone,javascript,backbone.js,leaflet,Backbone,Javascript,Backbone.js,Leaflet,请参阅下面的更新代码… 我试图使用forEach语句按另一个集合的属性筛选一个集合。如何编写此代码以在forEach语句中维护对变量“this.neighborhoodiscollection”的引用。这可能不是一个问题,而是一个问题。我想,换句话说,我该如何将变量“this.neighborhoodiscollection”传递到我的forEach语句中。以下是我尝试采取的步骤: 我将获取我的故事集和邻居集的结果传递给路由器: $(document).ready(function() {
$(document).ready(function() {
var neighborhoodsCollection = new NeighborhoodsCollection();
neighborhoodsCollection.fetch({
success: function(){
var storyCollection = new StoryCollection();
storyCollection.fetch({
success: function () {
var app = new AppRouter(neighborhoodsCollection, storyCollection);
Backbone.history.start();
}
});
}
});
});
然后我将传入的参数设置为局部变量
initialize: function(neighborhoodsCollection, storyCollection){
this.neighborhoodsCollection = neighborhoodsCollection;
this.storyCollection = storyCollection;
}
在路由器内的另一个功能中,我根据邻居集合中的每个邻居属性检查当前故事集合中的邻居数组,如果存在匹配项,我将邻居集合对象添加到传单地图中
load_story:function (id) {
//get Stories model object by id
this.story = this.storyCollection.get(id);
var storyNeighborhoods = this.story.attributes.neighborhoods;
storyNeighborhoods.forEach(function(neighborhood){
var hood = this.neighborhoodsCollection.attributes;
var nabe = [];
var nabeGeo = [];
for(var i = 0; i < _.size(hood); i++){
nabe.push(this.neighborhoodsCollection.attributes[i].properties.neighborhood);
nabeGeo.push(this.neighborhoodsCollection.attributes[i]);
var filterNabe = $.inArray(neighborhood, nabe);
if(filterNabe > -1){
L.geoJson(nabeGeo).addTo(map);
}else{
console.log('not found')
}
}
});
}
以下是我的收藏中唯一对象的结构:
Object { cid: "c1", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object, _pending: false }
[{ "id": "Greenpoint", "type": "Feature", "properties": { "neighborhood": "Greenpoint", "boroughCode": "3", "borough": "Brooklyn" ... }]
但是,我无法在此集合上成功执行get(id)。也许这与需要从集合中的每个对象创建模型有关,或者类似的事情
以下是基于@Quince示例的相关代码,仅供参考:
var neighborhoodsCollection = this.neighborhoodsCollection;
var neighborhoodsCollectionObjects = this.neighborhoodsCollection.attributes;
var neighborhoodsCollectionProperties = _.pluck(neighborhoodsCollectionObjects, 'properties');
var neighborhoodsCollectionArray = _.pluck(neighborhoodsCollectionProperties, 'neighborhood');
var storyCollectionNeighborhoods = this.story.attributes.neighborhoods;
var neighborhoodIntersection = _.intersection(storyCollectionNeighborhoods, neighborhoodsCollectionArray);
NeighboryIntersection的值是一个包含单个字符串“Greenpoint”的数组
我希望我正确理解了这一点,你想浏览每个故事,如果它有一个邻里与主要邻里集合中的一个邻里相匹配,那么你想获取它的引用吗 这样做的方法是在比较故事的邻域集合中的id和主邻域集合中的id时使用下划线相交函数。然后使用此列表从主邻居集合中获取实际引用。下面是我的意思()
我刚才看到的一个问题是uu.sortedIndex,尽管数组只包含匹配邻域的唯一id,但在添加已匹配邻域时,仍然需要时间将值右移。这确实让我走上了正确的道路。现在我有了一个数组,其中包含我将用于在集合上执行.get的每个匹配邻域字符串。这就是我遇到麻烦的地方。我将在上面的问题中添加我当前的代码作为更新,也许你可以看看?你的邻居集合的结构看起来很奇怪,它是主干集合吗?因为它看起来更像一个模型。通常情况下,如果我记录我的集合{length:4,models:Array[4],_byId:Object,constructor:function,model:function…}的话,这就是我的集合的样子。实际上,我还注意到您使用了一行--var neighborhoodiscollectionobjects=this.neighborhoodiscollection.attributes;但是,如果这是一个集合,并且您想从中获取模型,那么您可以使用this.neighboryscollection.models作为主干集合,因为它没有属性,所以get无法工作。你需要将你的邻里集合定义为一个集合,它有一个邻里模型作为它的模型好的,我发现了我的错误,当然,这是一个粗心的错误:(我将我的模型和集合都定义为Backbone.model.extend。一旦我将集合变量更改为equal Backbone.collection.extend,集合就有了正确的结构,@Quince answer正是我所需要的。它工作得很好,足以隐藏我的错误,直到它没有,我不得不将错误追溯到版本。)这是一个很好的开始。
neighborhoodIntersection.forEach(function(neighborhood){
neighborhoodsCollection.get(neighborhood);
});
//setup of models and collections
var NeighbourHoodModel = Backbone.Model.extend({
defaults:{
name: null
}
});
var StoryModel = Backbone.Model.extend({
defaults:{
neighbourhoods:null
}
});
var NeighborhoodsCollection = Backbone.Collection.extend({
model: NeighbourHoodModel
});
var StoryCollection = Backbone.Collection.extend({
model: StoryModel
})
var neighbourHoods = new NeighborhoodsCollection([
{ id:1,name: "neighbourhood-1"},
{ id:2,name: "neighbourhood-2"},
{ id:3,name: "neighbourhood-3"},
{ id:4,name: "neighbourhood-4"}
]);
var stories = new StoryCollection([
{ id:1, neighbourhoods: new NeighborhoodsCollection([
{ id:1,name: "neighbourhood-1"},
{ id:2,name: "neighbourhood-2"}
]),
},
{ id:2, neighbourhoods: new NeighborhoodsCollection([
{ id:1,name: "neighbourhood-1"},
{ id:2,name: "neighbourhood-2"}
]),
}
]);
(function(neighbourHoods,stories){
var _matchedNeighbourhoodIds = [];
//grab an array (usign pluck) of all the nighboorhood ids
//do this here so don;t have to do it on every loop
var neighbourhoodIds = neighbourHoods.pluck("id");
stories.forEach(function(story){
//for each story grab an array (using pluck) of all the stories neighborhoods
//perform an intersection of the two arrays (returning an array of id's that appear in both lists)
//slice that array
//for each element add it to the _matchNeighbourhoods in at a sorted index (this is so that we don't end with duplicates in the array)
_.intersection(story.get("neighbourhoods").pluck("id"),neighbourhoodIds).slice(0).forEach(function(neighbourhoodId){
_matchedNeighbourhoodIds[_.sortedIndex(_matchedNeighbourhoodIds,neighbourhoodId)] = neighbourhoodId;
});
});
_matchedNeighbourhoodIds.forEach(function(matchedNeighbourHoodId){
console.log(neighbourHoods.get(matchedNeighbourHoodId));
});
})(neighbourHoods,stories);