secret
Created

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
(function() {
 
var Mixins = {
sync: function(method, model, options) {
console.debug('Backbone.sync("%s", %o, %o);',method, model, options);
console.debug('this:%o',this);
$.ajax({
type: 'POST',
headers: {
"Accept": "*/*",
"X-Ajax-Synchronization-Token": this.area.ajax_synchronization_token,
"X-Authorization": "Basic " + this.area.jsproxy
},
url: '/proxy?apikey=' + this.user.key + '&sig=' + this.user.signature,
data: JSON.stringify({
method: model.method,
id: 1,
params: model.params
}),
dataType: "json",
success: options.success,
error: options.error
});
}
};
var MyLibrary = Backbone.Model.extend({
url: '/data/config.js',
initialize: function(config) {
this.session = new Session();
this.trigger("loaded");
}
});
var Session = Backbone.Model.extend({
frequency: 60, // in seconds
method: "area.fetch",
params: [],
initialize: function() {
this.start();
},
start: function() {
this.monitor = setInterval(this.fetch({
success: function(session, response) {
console.debug("response: %o",response);
},
error: function(session, response) {
console.debug("response: %o",response);
}
}),this.frequency*1000);
},
sync: function(method, model, options) {
Mixins.sync(method, model, options);
}
});
 
 
// Establish the root object, `window` in the browser, or `global` on the server.
var root = this;
 
// Save the previous value of the `MyLibrary` variable.
var previousMyLibrary = root.MyLibrary;
 
// Export the MyLibrary object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `MyLibrary` as a global object via a string identifier,
// for Closure Compiler "advanced" mode.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = MyLibrary;
}
exports.MyLibrary = MyLibrary;
} else {
root['MyLibrary'] = MyLibrary;
}
 
// Run MyLibrary.js in *noConflict* mode, returning the `MyLibrary` variable to its
// previous owner. Returns a reference to the Underscore object.
MyLibrary.noConflict = function() {
root.MyLibrary = previousMyLibrary;
return this;
};
 
 
}).call(this);
 
 
window.mylibrary = new MyLibrary({
"area": {
"ajax_synchronization_token": "sometoken",
"jsproxy": "proxytoken"
},
"user": {
"key": "somekey",
"signature": "somesignature"
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.