Skip to content

Instantly share code, notes, and snippets.

@codeboyim
Last active December 27, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeboyim/7251665 to your computer and use it in GitHub Desktop.
Save codeboyim/7251665 to your computer and use it in GitHub Desktop.
A backbonejs Router sample
(function () {
var router = null, view = null;
$(function () {
RegisterRouter = Backbone.Router.extend({
//setup route maps
routes: {
"": "list",
"list/:key/:type": "list"
},
initialize: function (options) {
_.bindAll(this, "remote_succeeded", "remote_failed");
this.view = options.view;
this.view.router = this;
this.model = this.view.model;
PageMethods.set_defaultSucceededCallback(this.remote_succeeded);
PageMethods.set_defaultFailedCallback(this.remote_failed);
},
//handle routes, connect url to action
list: function (key, type) {
var items, o;
key = key || this.model.get("key");
o = _.clone(this.model.toJSON());
items = o[key].items || [];
type = type || o[key]["type"];
if (type !== o[key]["type"]) {
items = [];
}
o["key"] = key;
o[key]["type"] = type;
this.model.set(o, { silent: true });
this.abortWebMethods();
if (!items || items.length === 0) {
switch (key) {
case "mykey":
$GLOBAL$AddToWsRequests(PageMethods._staticInstance.GetList(type));
break;
default:
break;
}
}
},
save: function (data, cb) {
$GLOBAL$AddToWsRequests(PageMethods._staticInstance.SaveRecord(data, this.model.get((this.model.get("key")))["type"], null, null, { callback: cb, update: data.Id !== 0 }));
},
get: function (id, cb) {
$GLOBAL$AddToWsRequests(PageMethods._staticInstance.GetRecord(id, null, null, { callback: cb }));
},
del: function (id, cb) {
$GLOBAL$AddToWsRequests(PageMethods._staticInstance.DelRecord(id, this.model.get((this.model.get("key")))["type"], null, null, { callback: cb }));
},
getList: function (cb) {
$GLOBAL$AddToWsRequests(PageMethods._staticInstance.GetList(this.model.get((this.model.get("key")))["type"], null, null, { callback: cb }));
},
abortWebMethods: function () {
$GLOBAL$AbortWebMethod("GetList");
$GLOBAL$AbortWebMethod("SaveRecord");
$GLOBAL$AbortWebMethod("GetRecord");
$GLOBAL$AbortWebMethod("DelRecord");
},
remote_succeeded: function (r, c, m) {
var result = r["result"], o, errorReturned;
errorReturned = !$GLOBAL$FilterPageMethodsMessages(r)
if (!errorReturned) {
if (m === "GetList" || m === "SaveRecord" || m === "DelRecord") {
key = this.model.get("key");
o = _.clone(this.model.toJSON());
o[key].items = result ? result : [];
this.model.set(o);
}
else if (m === "GetRecord") {
this.view.get_complianceView().model.set(result, { silent: true });
this.view.get_complianceView().render();
}
}
if (c && c.callback && c.callback.constructor === Function) {
c.callback.apply(null, [{ failed: errorReturned }]);
}
},
remote_failed: function (err, c, m) {
if (c && c.constructor === Function) {
c.apply(null, [{ failed: true }]);
}
if (!$GLOBAL$CheckWebMethodAborted(m))
$GLOBAL$displayMessage($GLOBAL$CreateSystemErrorMessage(err.get_message()));
}
});
});
function init_Register() {
if (!router && RegisterRouter) {
view = new RegisterView({ model: new RegisterViewModel() });
router = new RegisterRouter({ view: view });
Backbone.history.start();
}
}
$(function () {
init_Register();
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment