【问题标题】:Bootstrap Typeahead with Parse.com使用 Parse.com 引导 Typeahead
【发布时间】:2016-03-21 11:20:28
【问题描述】:

我正在尝试将 typeahead 与 Parse.com 结果一起使用。我认为Parse 返回JSON 对象,但似乎以下方法无法识别它。任何人都可以发现有什么问题吗?或者也许有更简单的方法让它工作(最后我希望能够搜索每个对象的两个字段)。

目前为止(感谢:https://github.com/bassjobsen/Bootstrap-3-Typeahead):

function queryListy(){
    Parse.Cloud.run('queryList', {}, {
        success: function(result) {      
          var $input = $('#query');       
          $input.typeahead({source:result, 
            autoSelect: true}); 
          $input.change(function() {
                var current = $input.typeahead("getActive");
                if (current) {
                    // Some item from your model is active!
                    if (current.name == $input.val()) {
                        // This means the exact match is found. Use toLowerCase() if you want case insensitive match.
                    } else {
                        // This means it is only a partial match, you can either add a new item 
                        // or take the active if you don't want new items
                    }
                } else {
                    // Nothing is active so it is a new value (or maybe empty value)
                }
            });
        },
        error: function(error) {
           console.log(error);
          }
    });
}

当我输入 [{id: "someId1", name: "Display name 1"}, {id: "someId2", name: "Display name 2"}] 而不是 result 时,它可以正常工作,所以基本机制似乎是正确的。

【问题讨论】:

    标签: javascript json twitter-bootstrap parse-platform bootstrap-typeahead


    【解决方案1】:

    Parse 不会以您可以直接使用的形式返回对象。您需要在需要的每个属性上调用get

    var mappedObjects = [];
    result.forEach(function(parseObject){
      mappedObjects.push({
        id: parseObject.id, //id is special, you don't need get
        name: parseObject.get("name"), //generic fields need get
      });
    });
    $input.typeahead({source:mappedObjects, ...
    

    如果您愿意,您可以在您的云代码中执行此映射,这样您的客户端代码就不需要了。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2023-03-20
      • 2015-02-05
      相关资源
      最近更新 更多