继续讨论:RactiveJS and jQuery plugins我能够将一个jquery插件包装成一个ractive装饰器。您将看到我使用行data.resources.splice(newIndex,0,data.resources.splice(oldIndex,1)[0])硬连接/操纵数据数组;
这是蛮力。理想情况下,我希望能够从节点中检索密钥路径。
我很难弄清楚如何获得所选节点使用的密钥路径......它由data.resources填充。我已尝试过ractive.get() - 但它会从根目录返回所有内容('数据')。
是否有方法会返回(' data.resources')
var sorttable = function (node) {
var oldIndex;
var newIndex;
$(node).sortable({
containerSelector: 'table',
itemPath: '> tbody',
itemSelector: 'tr',
placeholder: '<tr class="placeholder"/>',
onDragStart: function ($item) {
oldIndex = $item.index();
console.log(oldIndex , newIndex);
},
onDrop: function ($item) {
newIndex = $item.index();
if(newIndex != oldIndex) {
console.log(oldIndex , newIndex);
data.resources.splice(newIndex, 0, data.resources.splice(oldIndex, 1)[0] );
}
}
});
return {
teardown: function () {
$(node).sorttable('destroy');
}
};
};
Ractive.decorators.sortable = sorttable;
*****编辑*****
var sorttable = function (node) {
var oldIndex;
var newIndex;
$(node).sortable({
containerSelector: 'table',
itemPath: '> tbody',
itemSelector: 'tr',
placeholder: '<tr class="placeholder"/>',
onDragStart: function ($item) {
oldIndex = $item.index();
console.log(oldIndex , newIndex);
},
onDrop: function ($item) {
newIndex = $item.index();
var info = $item.context.parentNode;
info = Ractive.getNodeInfo(info);
var sourceKeypath = info.keypath;
var lastDotIndex = sourceKeypath.lastIndexOf( '.' );
var sourceArray = sourceKeypath.substr( 0, lastDotIndex );
if(newIndex != oldIndex) {
console.log(oldIndex , newIndex);
data[sourceArray].splice(newIndex, 0, data[sourceArray].splice(oldIndex, 1)[0] );
}
}
});
return {
teardown: function () {
$(node).sortable('destroy');
}
};
};
由于我的设置方式,我不得不做一些奇怪的遍历......我必须找到TR,从中我能够获得密钥路径。但是,我仍然需要输入数据。&#39;之前...所以现在就把它弄清楚了。
*****编辑2 - 这有效! *****
var sortTable = function (node) {
var oldIndex;
var newIndex;
$(node).sortable({
containerSelector: 'table',
itemPath: '> tbody',
itemSelector: 'tr',
placeholder: '<tr class="placeholder"/>',
onDragStart: function ($item) {
// Get index of our 'grabbed' item
oldIndex = $item.index();
console.log(oldIndex , newIndex);
},
onDrop: function ($item) {
// Get index of our 'dropped' item
newIndex = $item.index();
// Get the parentNode (this would be the tr)
var info = $item.context.parentNode;
// Then get all the Ractive info about this TR (gives us the keypath etc)
info = Ractive.getNodeInfo(info);
// Put the keypath into its own variable
var sourceKeypath = info.keypath;
// The keypath returns the array, plus the index of the item.
// We don't need the index - only the name of the array
var lastDotIndex = sourceKeypath.lastIndexOf( '.' );
var sourceArray = sourceKeypath.substr( 0, lastDotIndex );
// We need the parentObj so we can do parentObj[sourceArray]
// nb: data[resources]
// So grab the parent, and stick it into its own var
var parentObj = ractive.get( sourceKeypath.parent );
// Then update the array with the new order here
if(newIndex != oldIndex) {
console.log(oldIndex , newIndex);
parentObj[sourceArray].splice(newIndex, 0, parentObj[sourceArray].splice(oldIndex, 1)[0] );
}
}
});
return {
teardown: function () {
$(node).sortable('destroy');
}
};
};
答案 0 :(得分:2)
var info = Ractive.getNodeInfo(node);
(参见:http://docs.ractivejs.org/latest/ractive-getnodeinfo)
为您提供{ ractive: instance, keypath: keypath, index: indices }
,其中keypath
是节点的上下文,如果它位于{{#each}}
中,则类似resources.0
。
然后您可以执行以下操作:
lastDotIndex = sourceKeypath.lastIndexOf( '.' );
sourceArray = sourceKeypath.substr( 0, lastDotIndex );
sourceIndex = +( sourceKeypath.substring( lastDotIndex + 1 ) );
获取数组(上面来自ractive-decorators-sortable)