Mixing Google Maps custom overlays with Backbone Views

后端 未结 2 1875
你的背包
你的背包 2021-01-01 01:04

TL;DR

Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the \"proper\" way to have a Backbone View

2条回答
  •  盖世英雄少女心
    2021-01-01 01:32

    Ah! So I've been doing the above, but it's never felt right.

    Then I found this discussion on a Backbone group which leads me to the following:

    var MyView = (function(){
        var view = function(){
            Backbone.View.apply(this, arguments);
        };
    
        view.extend = Backbone.View.extend;
    
        _.extend(view.prototype, Backbone.View.prototype, google.maps.OverlayView.prototype, [other prototypes...], { [VIEW DEFINITION] });
    
        return view;
    }());
    

    This way if we need to override any of the definitions in a class we're extending from, we can since it's earlier in the _.extend chain (later definitions overwrite earlier definitions).

    I'm working on 'extending' extend to keep track of the "parent" object's references that would be overridden and providing a method to call them still (like Python's super call). I haven't decided if this should be done through monkey-patching, an intercepter pattern (via underscore's _.tap() method or something else, but I think it'll add a lot of flexibility.

    This would allow you to define an initialize view in your "parent" class which could be called by doing something like _.super('ParentClass', 'initialize'); at the end of the "child" class's initialize routine...

提交回复
热议问题