使用键值对内的方法截断字符串

时间:2016-11-10 18:32:41

标签: javascript lodash truncate keyvaluepair

我在我的javascript中使用了Lodash和jQuery库,我试图弄清楚如何调用一个方法,允许我截断用于在我的.html代码中创建列表的键值对的结果。 html看起来如下:

 <div class="slide-in-panel">
        <ul class="list-unstyled slide-in-menu-navigation" data-bind="foreach: __plugins">
            <li class="btn-block">
                <div class="btn btn-default btn-block" data-bind="click: $parent.showPlugin, tooltip: 'Shoebox.Panel'">
                    <span data-bind="text: config.title"></span>
                    <em class="webgis-Icon webgis-Cross slide-in-menu-remove-shoebox-button"
                        data-bind="click: $parent.showRemoveConfirmBox, tooltip: 'Shoebox.RemoveShoebox'">
                    </em>
                </div>
            </li>
        </ul>
 </div>

关键部分是data-bind="text: config.title"部分。这将使用该按钮的名称填充列表。 config.title是在下面的javascript文件中创建的。我的目标是将诸如.truncate()之类的方法应用于javascript中的config.title部分,以保留正在填充的任何名称,从长到长。我该怎么做?

 return this.__backendShoeboxClient.createShoebox(this.__shoeboxName()).then((function(_this) {
      return function(shoebox) {
        return $when.join(shoebox.getName(), shoebox.getId(), shoebox.getUserName()).then(function(arg) {
          var shoeboxId, shoeboxName, userName;
          shoeboxName = arg[0], shoeboxId = arg[1], userName = arg[2];
          return _this.__shoeboxContentFactory.create({
            shoeboxId: shoeboxId,
            shoeboxName: shoeboxName,
            userName: userName
          }).then(function(arg1) {
            var activeShoeboxHandle, config, shoeboxContent;
            shoeboxContent = arg1.shoeboxContent, activeShoeboxHandle = arg1.activeShoeboxHandle;
            _this.__activeShoeboxHandleMain.loadModel(activeShoeboxHandle);
            config = {
              plugin: shoeboxContent,
              title: shoeboxName,
              userName: userName,
              id: shoeboxId,
              handle: activeShoeboxHandle,
              icon: ""
            };
            _this.add(config, null, null);
            activeShoeboxHandle.loadModel(shoebox);
            _this.__shoeboxName.useDefaultValue();
            return _this.__shoeboxName.clearError();
          });
        })["catch"](function(error) {
          __logger__.error("Error while calling request " + error);
          return $when.reject(new Error("Error while calling request. " + error));
        });
      };
    })(this));
  };

我也试图像这样使用淘汰赛style绑定,但没有任何成功:

<span data-bind="style: { textOverflow: ellipsis }, text: config.title"></span>

2 个答案:

答案 0 :(得分:0)

这应该这样做: 使用截断函数如下:config.title = _.truncate(config.title,{&#39; length&#39;:maxLength});

return this.__backendShoeboxClient.createShoebox(this.__shoeboxName()).then((function(_this) {
  return function(shoebox) {
    return $when.join(shoebox.getName(), shoebox.getId(), shoebox.getUserName()).then(function(arg) {
      var shoeboxId, shoeboxName, userName;
      shoeboxName = arg[0], shoeboxId = arg[1], userName = arg[2];
      return _this.__shoeboxContentFactory.create({
        shoeboxId: shoeboxId,
        shoeboxName: shoeboxName,
        userName: userName
      }).then(function(arg1) {
        var activeShoeboxHandle, config, shoeboxContent;
        shoeboxContent = arg1.shoeboxContent, activeShoeboxHandle = arg1.activeShoeboxHandle;
        _this.__activeShoeboxHandleMain.loadModel(activeShoeboxHandle);
        config = {
          plugin: shoeboxContent,
          title: shoeboxName,
          userName: userName,
          id: shoeboxId,
          handle: activeShoeboxHandle,
          icon: ""
        };
        config.title = _.truncate(config.title, {'length': 15});
        _this.add(config, null, null);
        activeShoeboxHandle.loadModel(shoebox);
        _this.__shoeboxName.useDefaultValue();
        return _this.__shoeboxName.clearError();
      });
    })["catch"](function(error) {
      __logger__.error("Error while calling request " + error);
      return $when.reject(new Error("Error while calling request. " + error));
    });
  };
})(this));

};

答案 1 :(得分:0)

因此,为了启发,我能够在简单的if语句中使用substring方法找到解决此问题的方法。问题似乎是我把它放在我的代码的错误部分,所以我想澄清一下对我未来读者有用的东西。我能够在密钥:值对中应用以下内容,它完全有效:

config =
    plugin: shoeboxContent
    title: if name.length > 24
        "#{name.substring 0, 24}..."
    else
        name

    userName: shoebox.getUserName()
    id: shoebox.getId()
    handle: activeShoeboxHandle
    icon: ""

@add config, null, null