node.js - "Sandbox" for using multiple instances of a javascript lib -
i'm using js lib, create global variable "av" used everywhere in webapp. want create "sandbox"(not strict sandbox because of no secure concern) utilize multiple different "av"s in 1 webapp.
i wrote wrap browser below , works.
var avcontexts = { app1: null, app2: null } var contextloader = function (appid, appkey) { this.av = null; this.runinthis = function (script) { eval(this.script); this.av.initialize(appid, appkey); } this.loadcontext = function (appid, appkey) { $.ajax({ url: 'js/av.js', datatype: "text", context: }).done(function (data) { this.script = data; this.runinthis.call(this); }).fail(function () { console.log('failed'); }); } this.loadcontext(appid, appkey); } avcontexts.app1 = new contextloader( "[appid]", "[appkey]" ); avcontexts.app2 = new contextloader( "[appid]", "[appkey]" ); // var testobject = avcontexts.app1.av.object.extend("testobject"); var testobject = new testobject(); testobject.save({foo: "bar"}, { success: function (object) { alert("avos cloud works!"); } });
but when move nodejs. error occurred @
this.runinthis.call(this); error: cannot phone call method 'call' of undefined
any idea?
you should cache "this", this:
var me = this;
before utilize $.ajax. , then,
$.ajax().done(function() { me.runinthis.call(); });
if want know reason,
run
console.log(this);
before
this.runinthis.call(this);
you know reason.
javascript node.js
No comments:
Post a Comment