Debugging Javascript (Backbone and Marionette)

后端 未结 4 1646
余生分开走
余生分开走 2021-02-01 08:24

Right now, while I am debugging backbone or marionette using the chrome dev tools, I end up setting break points and whatnot, but once the code pauses, its hard to tell what typ

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 08:51

    Yes. You can change the console display name by overriding a model/collection/view constructor using a named function expression. It may also be helpul to override toString to control the console output when the model is forced to string type with, say, the + operator:

    App.Model = Backbone.Model.extend({
    
      //define constructor using a named function expression
      constructor: function Model() {
        Backbone.Model.prototype.constructor.apply(this, arguments);
      },
    
      //override toString to return something more meaningful
      toString: function() {
        return "Model(" + JSON.stringify(this.attributes) + ")";
      }
    });
    

    So with:

    var model = new Model({id:1,foo:"bar"})
    
    console.log("state: " + model);
    console.log(model);
    

    You'll get:

    state: Model({"id":1,"foo":"bar"})
    ► Model
    

提交回复
热议问题