Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 击倒JS构建项目列表_Javascript_Design Patterns_Mvvm_Knockout.js - Fatal编程技术网

Javascript 击倒JS构建项目列表

Javascript 击倒JS构建项目列表,javascript,design-patterns,mvvm,knockout.js,Javascript,Design Patterns,Mvvm,Knockout.js,我试图做的是从其他更大的可用项目列表中建立一个项目列表(例如,考虑指定某种类型产品的成分)。当我遇到淘汰赛的时候,我还是很生疏,我想知道我是否遗漏了什么,因为我目前使用的解决方案感觉是错误的。我有两个非常标准的模板,我在视图模型上对两个可观察的数组使用foreach绑定(一个用于可用成分列表,另一个用于产品的选定成分列表) var productTypeViewModel={ IngCreditTypes:ko.observableArray([]), 选择的灰色类型:ko.observear

我试图做的是从其他更大的可用项目列表中建立一个项目列表(例如,考虑指定某种类型产品的成分)。当我遇到淘汰赛的时候,我还是很生疏,我想知道我是否遗漏了什么,因为我目前使用的解决方案感觉是错误的。我有两个非常标准的模板,我在视图模型上对两个可观察的数组使用foreach绑定(一个用于可用成分列表,另一个用于产品的选定成分列表)


var productTypeViewModel={
IngCreditTypes:ko.observableArray([]),
选择的灰色类型:ko.observearray([]),
附加元件:函数(){
productTypeViewModel.IngCreditTypes.remove(此);
productTypeViewModel.SelectedIngredEntTypes.push(此);
},
RemoveCredit:函数(){
productTypeViewModel.SelectedIngredEntTypes.remove(此);
productTypeViewModel.IngCreditTypes.push(此);
},
};
$(文档).ready(函数(){
$.getJSON(“/IngreditType/Index”)
.成功(功能(数据){
productTypeViewModel.IngreditTypes($.parseJSON(数据));
})
.error(函数(){alert(“error”);});
应用绑定(productTypeViewModel);
});
${Name}
这是我想用击倒的最好方法吗?感觉有点不对劲,虽然我不太清楚是什么。我确实为IngreditType提供了一个单独的VM定义,其中有一个add函数。ingredientTypes observable将使用这种类型的虚拟机数组进行初始化,然后模板只在其上使用数据绑定“click:add”,但我不喜欢必须从处理click的主虚拟机返回通信


基本上,我只是想表明我所做的是一种标准方法还是有更好的(更适合淘汰赛的)方法。我认为你的方法是完全有效的,这是我在其他淘汰赛示例中看到的。例如,从Ryan Niemeyer的v1中,您可以找到与您所做的非常相似的以下代码片段:

if (position >= 0) {
    originalParent.remove(item);
    newParent.splice(position, 0, item);
}
if (position >= 0) {
    originalParent.remove(item);
    newParent.splice(position, 0, item);
}