使用Parse.com进行Bootstrap Typeahead

时间:2015-12-15 15:29:29

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

我试图将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时,它的工作基本机制似乎是正确的。

1 个答案:

答案 0 :(得分:2)

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, ...

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