【发布时间】:2015-07-02 20:23:28
【问题描述】:
从这个讨论开始:RactiveJS and jQuery plugins 我能够将一个 jquery 插件包装到一个 ractive 装饰器中。你会看到我用 data.resources.splice(newIndex, 0, data.resources.splice(oldIndex, 1)[0] );
这是蛮力。理想情况下,我希望能够从节点检索密钥路径。
我很难弄清楚如何获取所选节点使用的密钥路径...它由 data.resources 填充。我试过 ractive.get() - 但它会从根('data')返回所有内容。
是否有方法会返回('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,然后我才能获得密钥路径。但是,我仍然需要输入“数据”。之前...所以现在就弄清楚这一点。
***** EDIT 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');
}
};
};
【问题讨论】:
标签: javascript jquery ractivejs