解析数据库JavaScript和PhoneGap

时间:2015-12-10 00:54:55

标签: javascript html cordova parse-platform

我一直在研究应该从在线数据库获取菜单的餐馆应用程序。

这就是我目前和“手动”填充菜单的方式:

Controller.html

$scope.menu = [{
                     name: 'Espresso',
                     price: 27,
                     qty: 1,
                     desc: "One shot of espresso prepared with 7 grams of ground coffee in a single portafilter. The shot should be 1 ounce of liquid. You have two choices with espresso: ristretto, a very short or “restrained” shot, brewed at less than 2/3 of a demitasse, or luongo, a long pull of espresso brewed so the liquid should be more than 2/3 of a demitasse.",
                     img: "img/espresso.png",
                     active: false,
                     sizes: [{name: "Small", price: 0, active:false},
                             {name: "Medium", price: 5, active:false},
                             {name: "Large", price: 10, active:false}],
                     flavors: [{name: 'Vanilla', price: 8, active: false},
                               {name: 'Almond', price: 8, active: false},
                               {name: 'Hazelnut', price: 8, active: false},
                               {name: 'Caramel', price: 8, active: false}]
                     }]; 

但是我似乎无法使用Parse实现填充,我将如何使用查询来解决此问题(这是一个有效的查询)。

的index.html

<script type="text/javascript">

  Parse.initialize("vGoJDvwwfZBiUFcwfkee7M5vCqL7lLxCgKIFJXDc", "6VRlos6qppaek1uDPPLqpHtmB3fHefOJMqYJNxj9");

  var DrinkMenu = Parse.Object.extend("DrinkMenu");
  var query = new Parse.Query(DrinkMenu);
  query.find({
    success: function(results) {
      alert("Successfully retrieved " + results.length + " items.");
      // Do something with the returned Parse.Object values
      for (var i = 0; i < results.length; i++) {
        var object = results[i];
        alert(object.id + ' - ' + object.get('name'));
      }
    },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
    }
  });

</script>

您可以注意到我可以获取每个项目所需的变量,在这种情况下是第一个结果的名称,我在警报中显示。

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在Parse.initialize之后,创建一个变量,如下所示:

var arrMenu = [];

然后更改行警报(object.id +&#39; - &#39; + object.get(&#39; name&#39;));到

arrMenu.push({
                     name: object.get('name'),
                     price: object.get('price'),
                     qty: object.get('qty'),
                     desc: object.get('desc'),
                     img: object.get('img'),
                     active: object.get('active'),
                     sizes: object.get('sizes'),
                     flavors: object.get('flavor')
          });

我假设您使用您提到的结构将信息存储在Parse Collection中。如果不同,请告诉我。

然后,在关闭之后,你添加:

$scope.menu = arrMenu;

我希望它有所帮助!