Backbone.js singular Models not in collection

后端 未结 2 1435
慢半拍i
慢半拍i 2021-02-19 21:57

Being new to Backbone.js, just wanted to clarify the correct way to go about this simple task.

Developing a web app, almost always, you\'ll have user accounts, where use

相关标签:
2条回答
  • 2021-02-19 22:27

    There is not any reason to feel forced to declare a Collection for every Model your App has. It is very common to have Models without Collection associated.

    // code simplified and not tested
    App.CurrentUser = Backbone.Model.extend({
      url: "http://myapp.com/session.json"
    });
    
    App.CurrentUserView = Backbone.View.extend({
      el: "#user-info",
    
      render: function(){
        this.$el.html( "<h1>" + this.model.get( "name" ) + "</h1>" );
      }
    });
    
    var currentUser = new App.CurrentUser();
    currentUser.fetch();
    
    var currentUserView = new App.CurrentUserView()({ model: currentUser });
    currentUserView.render();
    
    0 讨论(0)
  • 2021-02-19 22:38

    If you want a model / view with no collection, don't write a collection or collection view.

    Add the view the same way you normally see the collection based view added.

    0 讨论(0)
提交回复
热议问题