把手选择列表

时间:2017-04-14 23:09:53

标签: javascript jquery backbone.js handlebars.js

我正在尝试列出我的数据来选择选项。我使用了主干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

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

2 个答案:

答案 0 :(得分:2)

由于您在{{/} }.

之后给出了错误的帮助名称,因此收到错误
{{#each users}}
  <option value='{{userID}}'>{{username}}</option>
{{/users}}

应该更正

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

答案 1 :(得分:0)

您已将视图命名为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
  };
)};