【问题标题】:Handlebars Select List车把选择列表
【发布时间】:2017-04-14 23:09:52
【问题描述】:

我正在尝试列出我的数据以选择选项。我正在使用车把主干 jquery requireJS。

这是我的模板

    <select id="userSelect">
<option value="" selected="selected">Kullanıcı Seçiniz</option>
{{#each users}}
<option value='{{userID}}'>{{username}}</option>
{{/users}}
</select>

我的观点

var users = new User();
var SelectUserList = Backbone.View.extend({
    model: User,
    el:'.page',
    render:function(users)
    {
        var template = Handlebars.compile(SelectUserList);
        var html = template({users:users.toJSON()});
        $("#userSelect").html(html);
        this.$el.html(SelectUserList);
        return this;

    }
});

return {
    users:users,
    SelectUserList:SelectUserList
};

还有我的 Router.js

  user: function () {
        var spinner = new Spinner();
        $('body').after(spinner.spin().el);
        var users = new Users();
        var userSelect = new UserSelectList.SelectUserList();
        users.fetch({
            contentType: "application/json",
            error: function () {
                console.log("error");
            },

            cache: false,
            success: function (m_users) {

                userSelect.users=m_users;
             userSelect.render(UserSelectList.users);
             disposeView(new UserSelectList.SelectUserList().render());

                }

        });

DisposeView 函数

    function disposeView(view) {
    var current = this.currentView;
    if (current) current.close();
    current = this.currentView = view;
    current.delegateEvents();
    return current;
}

我有这个错误My Error

我该如何解决?我的错在哪里? 谢谢

【问题讨论】:

    标签: javascript jquery backbone.js handlebars.js


    【解决方案1】:

    您收到错误是因为您在 {{/}} 之后提供了不正确的助手名称。

    {{#each users}}
      <option value='{{userID}}'>{{username}}</option>
    {{/users}}
    

    应该改成

    {{#each users}} 
     <option value='{{userID}}'>{{username}}</option> 
    {{/each}} 
    

    【讨论】:

    • 我编辑了这个语法错误,但是选择了填充 null。但是 console.log 返回了我的数据。 pasteboard.co/5xvUGvaor.png
    • 您是 SelectUserList 视图的渲染功能需要用户,并且您没有在渲染期间将它们传递给视图,也不是在初始化时。这就是你在选择中得到任何东西的原因。
    【解决方案2】:

    您已将视图命名为 SelectUserList。在您看来,您尝试编译SelectUserList(这是您的视图而不是模板字符串),但您希望将其传递给您的模板。通常会有类似的东西;

    define(['text!templates/my_tempalte.hbs'],
      function(MyTemplateHbs){
        var users = new User();
        var SelectUserList = Backbone.View.extend({
          model: User,
          el:'.page',
          render:function(users)
          {
              var template = Handlebars.compile(MyTemplateHbs); //pass the tempalte
              var html = template({users:users.toJSON()});
              this.$el.html(html);
              return this;
    
          }
      });
    
      return {
          users:users,
          SelectUserList:SelectUserList
      };
    )};
    

    【讨论】:

    • 我编辑了这个解决方案,但现在出现同样的异常错误“每个用户都不匹配 - 3:7”
    • 哦刚刚也看到了 {{#each users}} 需要结束 {{/each}} 而不是 {{/users}}
    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 2014-03-14
    • 2019-01-05
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多