【问题标题】:Ractive JS and jquery plugins - accessing / manipulating dataRactive JS 和 jquery 插件 - 访问/操作数据
【发布时间】: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


    【解决方案1】:
    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

    【讨论】:

    • 我在玩 getNodeInfo - 但是,我得到的都是空的? Object {ractive: vb, keypath: "", index: Object}index: Objectkeypath: ""ractive: vb__proto__: ....} - 所以我一定做错了
    • 或....插件应用于tbody...从技术上讲,这不是我单击的每个内部的tr?也许这会导致问题
    • 标记为正确 - 因为 getNodeInfo 正是需要的。我仍然有问题 - 但那是由于我如何使用这个插件进行设置......在父元素上,而不是每个中的 tr (所以它无法弄清楚上下文等)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多