Backbone.js singular Models not in collection

后端 未结 2 1476
慢半拍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( "

    " + this.model.get( "name" ) + "

    " ); } }); var currentUser = new App.CurrentUser(); currentUser.fetch(); var currentUserView = new App.CurrentUserView()({ model: currentUser }); currentUserView.render();

提交回复
热议问题