1

我的应用程序使用它并且$itemKnockout 3.4.0. 我需要访问parentList,它存储被拖到另一个列表的项目的原始父元素。

下面的代码不会与最新版本的 Knockout 一起运行,而 jQuery 模板是它的依赖项之一。我正在寻找原因、解决方案或解决方法。

带有详细示例的 JSFiddle:http: //jsfiddle.net/piglin/UAcC7/1837/

Uncaught ReferenceError: Unable to process binding "template: function (){return { name:'rowTmpl',foreach:$data.children,templateOptions:{ parentList:$data.children}} }"
Message: Unable to process binding "template: function (){return { name:'cellTmpl',foreach:$data.children,templateOptions:{ parentList:$data.children}} }"
Message: Unable to process binding "sortableItem: function (){return { item:$data,parentList:$item.parentList} }"
Message: $item is not defined

ko.bindingHandlers.sortableList = {};
ko.bindingHandlers.sortableItem = {
  init: function(element, valueAccessor) {
    var options = valueAccessor();
  }
};
var viewModel = function() {
  var self = this;
  self.children = ko.observableArray(
    [{}]
  );
};
ko.applyBindings(new viewModel());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://rniemeyer.github.com/KnockMeOut/Scripts/jquery.tmpl.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>

<div data-bind="template: { name: 'tmpl', foreach: $data.children, templateOptions: { parentList: $data.children } }">
</div>

<script id="tmpl" type="text/html">
  <div data-bind="sortableItem: { item: $data, parentList: $item.parentList }">
  </div>
</script>

4

2 回答 2

2

似乎您的模板名称是“cellTmpl”,但在绑定中您称为名称:'tmpl'

于 2016-01-25T10:58:25.490 回答
0

真正的问题是jQuery templates在将 HTML 文档附加到我的 Knockout JS 页面时未加载插件。

我已经正确配置了 RequireJS,这似乎是 Knockout 生命周期的问题,我需要jQuery templates在阶段加载依赖项activate,我正在编写一个代码示例来实现这一点。

于 2016-01-26T09:09:07.363 回答