简体   繁体   English

将Handlebars.js与StackMob结合使用

[英]Using Handlebars.js with StackMob

What I'm trying to do is when the page is loaded it will show the user a list of all their "contacts". 我想做的是在页面加载后向用户显示所有“联系人”的列表。 There is a fair bit of code so I put it all HERE and below is just the load method. 有很多代码,因此我将其全部放在此处 ,下面仅是load方法。

$(window).load(function () {
        var Contacts = StackMob.Model.extend({ schemaName: 'contacts' });
        var myContacts = new Contacts();
        var q = new StackMob.Collection.Query();
        q.orderAsc('firstname'); //sort by firstname in ascending order
        myContacts.query(q, {
            success: function (model) {
                console.log(model.toJSON());

                for (var i = 0; i < model.length; i++) {
                    var data = ({
                        FirstName: model[i].attributes.firstname,
                        LastName: model[i].attributes.lastname,
                        Pno: model[i].attributes.phoneno,
                        Emails: model[i].attributes.email,
                        objIdel: model[i].contacts_id,
                        objIdeit: model[i].contacts_id
                    });
                    var template = Handlebars.compile($('#template').html());

                    var html = template(model);

                    $("#contacts").append(template(data));
                }
            },
            error: function (model, response) {
                console.debug(response);
            }
        });
    });

console.log(model.toJSON()); console.log(model.toJSON()); shows what I would expect but It doesn't seem to be getting into the for loop at all. 显示了我的期望,但似乎根本没有进入for循环。

EDIT: If i get rid of the loop and use the code below I get only one contact with no values in the inputs 编辑:如果我摆脱了循环,并使用下面的代码,我只会在输入中没有值的情况下得到一个联系人

var data = ({
                        FirstName: model.attributes.firstname,
                        LastName: model.attributes.lastname,
                        Pno: model.attributes.phoneno,
                        Emails: model.attributes.email,
                        objIdel: model.contacts_id,
                        objIdeit: model.contacts_id
                    });

EDIT: I was able to get the firstname of a contact using console.log(results.attributes[0]["firstname"]); 编辑:我能够使用console.log(results.attributes [0] [“ firstname”]);获得联系人的名字; the problem is I cant figure out why its not going into the loop. 问题是我不知道为什么它不进入循环。

I tested the code without the loop and it made a handlebars template of the first contact that worked as planed, but I cant figure out why it wont loop through them all. 我测试了没有循环的代码,它使按计划工作的第一个联系人的车把模板得以实现,但是我无法弄清楚为什么它不会循环遍历所有代码。 Link to a more up to date version of the code 链接到最新版本的代码

How about trying this ... I iterate over the jsonData to get each object. 怎么样尝试呢?我遍历jsonData来获取每个对象。 Not sure if handlebars expects a JSON object or the JSON string, so I output each to the console.log 不确定把手是否需要JSON对象或JSON字符串,因此我将它们输出到console.log

var Contact = StackMob.Model.extend({ schemaName: 'todo' });
var Contacts = StackMob.Collection.extend({ model: Contact });

var q = new StackMob.Collection.Query();    
q.orderAsc('name'); //sort by firstname in ascending order

var myContacts = new Contacts();
myContacts.query(q, {
  success: function (data) {

    jsonData = data.toJSON();

    for (var i = 0; i < data.length; i++) {
      var obj = jsonData[i];
      console.log(obj);
      console.log(JSON.stringify(obj));
    }

  },
  error: function (model, response) {
    console.debug(response);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM