javascript - How to create a new Backbone View using apply -
i need create new view instance arguments array. dont want call
new view(array)
i tried this solution. unfortunately dont work, means no arg passed initialize function. there away create new view passing in array have arguments single 1 time in initialize function?
you can accomplish bit of prototypal trickery:
function createview() { var args = arguments; //create mill function, can "this" context function factory() { homecoming yourviewclass.apply(this, args);} //assign mill prototype (essentially makes mill yourviewclass) factory.prototype = yourviewclass.prototype; //invoke mill function homecoming new factory(); }; var view1 = createview({model:model}, 1, 2, 3); var view2 = createview.apply(null, argumentarray);
a general solution instantiating "class" (constructor function) variable arguments:
function instantiate(ctor) { //strip first arg, pass rest arguments constructor var args = array.prototype.slice.call(arguments, 1); function factory() { homecoming ctor.apply(this, args);} factory.prototype = ctor.prototype; homecoming new factory(); }; //call straight var view1 = instantiate(yourviewclass, {model:model}, 1, 2, 3); //partially apply create mill specific view class var viewfactory = _.partial(instantiate, yourviewclass); var view2 = viewfactory({model:model}, 1, 2, 3);
javascript backbone.js
No comments:
Post a Comment