Javascript 主干:在路线之前
谷歌上有很多主题,但没有完整有效的例子 如何包装路由器的导航,以便在需要时检查条件并取消导航? 另外,我看到了u.wrap、覆盖主干网、路由器、原型、路由等的想法,但没有完整的例子。Javascript 主干:在路线之前,javascript,backbone.js,backbone-routing,Javascript,Backbone.js,Backbone Routing,谷歌上有很多主题,但没有完整有效的例子 如何包装路由器的导航,以便在需要时检查条件并取消导航? 另外,我看到了u.wrap、覆盖主干网、路由器、原型、路由等的想法,但没有完整的例子。 我也看到了,但不知道如何将其集成到我的应用程序中。您可以尝试覆盖Backbone.history.route: var bbhRoute = Backbone.history.route; Backbone.history.route = function(route, callback) { return b
我也看到了,但不知道如何将其集成到我的应用程序中。您可以尝试覆盖Backbone.history.route:
var bbhRoute = Backbone.history.route;
Backbone.history.route = function(route, callback) {
return bbhRoute.call(Backbone.history, route, function(fragment) {
// if (decide if you want to prevent navigation) {
// window.history.back(); // to ensure hash doesn't change
// return;
// }
// if you want to let it happen:
return callback.apply(this, arguments);
});
};
您可以尝试覆盖Backbone.history.route:
var bbhRoute = Backbone.history.route;
Backbone.history.route = function(route, callback) {
return bbhRoute.call(Backbone.history, route, function(fragment) {
// if (decide if you want to prevent navigation) {
// window.history.back(); // to ensure hash doesn't change
// return;
// }
// if you want to let it happen:
return callback.apply(this, arguments);
});
};
为了实现这一点,我扩展了主干路由器。我希望我的路由器能帮助你。 我添加了一个属性toevaluation,使用route的先决条件,并添加了evaluateRoutesFn,该函数将在导航某个路由之前对它们进行评估 我的路线示例如下:
var MySubRouter = Backbone.ModuleRoute.extend({
//hasPermissions is static
//if you want a function of an instance you can use a function instead
evaluateRoutesFn: SomeView.hasPermissions,
toEvaluate: {
routeA: {
modulePreconditions: [SomeView.CONDITION_A, ...]
}
},
routeA: function(param1, param2) {
//the route to navigate with preconditions
},
具有权限的方法返回true或false或错误
var MyView = ModuleView.extend({}, {
CONDITION_A: "conditionA",
hasPermissions: function (condition, properties) {
switch (condition) {
case MyView.CONDITION_A:
return app.ConditionA;
default:
return true;
}
}
});
更重要的是ModuleRoute。这是我的moduleRoute的基础。我添加了更多的东西。比如错误控制。我在我的应用程序的每个模块中保存了所有当前路线。。。还有更多。您可以添加任何需要的内容。它真的很有用
Backbone.ModuleRoute = Backbone.Router.extend({
evaluateRoutesFn: null,
toEvaluate: null,
constructor: function (prefix, options) {
this._finalRoutes = {};
Backbone.SubRoute.prototype.constructor.call(this, prefix, options);
},
onRoutes: function (route) {
var aps = Array.prototype.slice;
var args = aps.call(arguments);
args.shift();
var routeName = this.routes[route];
var evalResult = true;
try {
var toEval = this.toEvaluate && this.toEvaluate[routeName];
if (toEval) {
evalResult = this.evalPreconditions(toEval.modulePreconditions, args);
//more future preconditions
}
}
catch (err) {
evalResult = false;
}
if (evalResult)
try {
this._finalRoutes[route].apply(this, args);
}
catch (err) {
window.history.back(); //go back, error control...
}
else
window.history.back(); //no permissions, go back
},
evalPreconditions: function (preconds, args) {
if (!this.evaluateRoutesFn) {
throw "WARNING: Evaluate routes function must be overriden to evaluate them. " +
"Don't assign toEvaluate if you won't do it. The evaluation has been skipped.";
}
if (!preconds)
return true;
var evalResult = true;
for (var i = 0, len = preconds.length; i < len; i++) {
evalResult = this.evaluateRoutesFn(preconds[i], args);
if (!evalResult) {
throw "ERROR: The precondition is not truth.";
break;
}
}
return evalResult;
},
route: function (route, name, callback) {
this._finalRoutes[route] = (!callback) ? this[name] : callback;
var that = this;
callback = function (path) {
return function () {
var aps = Array.prototype.slice;
var args = aps.call(arguments);
args.unshift(path);
that.onRoutes.apply(that, args);
};
} (route);
return Backbone.Router.prototype.route.call(this, route, name, callback);
}
});
Backbone.ModuleRoute=Backbone.Router.extend({
evaluateRoutesFn:null,
toEvaluate:null,
构造函数:函数(前缀、选项){
这._finalRoutes={};
Backbone.subcure.prototype.constructor.call(this,prefix,options);
},
onRoutes:功能(路由){
var aps=Array.prototype.slice;
var args=aps.call(参数);
args.shift();
var routeName=this.routes[route];
var evalResult=真;
试一试{
var-toEval=this.toEvaluate&&this.toEvaluate[routeName];
如果(toEval){
evalResult=this.evalPreconditions(toEval.moduleReconditions,args);
//更多的未来先决条件
}
}
捕捉(错误){
evalResult=false;
}
if(evalResult)
试一试{
this.\u finalRoutes[route].apply(this,args);
}
捕捉(错误){
window.history.back();//返回,错误控制。。。
}
其他的
window.history.back();//没有权限,返回
},
evalPreconditions:功能(预编码、参数){
如果(!this.evaluateRoutesFn){
throw“警告:必须重写Evaluate routes函数才能对其进行评估。”+
“如果您不愿意,请不要分配给评估。评估已被跳过。”;
}
如果(!preconds)
返回true;
var evalResult=真;
对于(变量i=0,len=prectds.length;i
以获得扩展主干网路由器。我希望我的路由器能帮助你。
我添加了一个属性toevaluation,使用route的先决条件,并添加了evaluateRoutesFn,该函数将在导航某个路由之前对它们进行评估
我的路线示例如下:
var MySubRouter = Backbone.ModuleRoute.extend({
//hasPermissions is static
//if you want a function of an instance you can use a function instead
evaluateRoutesFn: SomeView.hasPermissions,
toEvaluate: {
routeA: {
modulePreconditions: [SomeView.CONDITION_A, ...]
}
},
routeA: function(param1, param2) {
//the route to navigate with preconditions
},
具有权限的方法返回true或false或错误
var MyView = ModuleView.extend({}, {
CONDITION_A: "conditionA",
hasPermissions: function (condition, properties) {
switch (condition) {
case MyView.CONDITION_A:
return app.ConditionA;
default:
return true;
}
}
});
更重要的是ModuleRoute。这是我的moduleRoute的基础。我添加了更多的东西。比如错误控制。我在我的应用程序的每个模块中保存了所有当前路线。。。还有更多。您可以添加任何需要的内容。它真的很有用
Backbone.ModuleRoute = Backbone.Router.extend({
evaluateRoutesFn: null,
toEvaluate: null,
constructor: function (prefix, options) {
this._finalRoutes = {};
Backbone.SubRoute.prototype.constructor.call(this, prefix, options);
},
onRoutes: function (route) {
var aps = Array.prototype.slice;
var args = aps.call(arguments);
args.shift();
var routeName = this.routes[route];
var evalResult = true;
try {
var toEval = this.toEvaluate && this.toEvaluate[routeName];
if (toEval) {
evalResult = this.evalPreconditions(toEval.modulePreconditions, args);
//more future preconditions
}
}
catch (err) {
evalResult = false;
}
if (evalResult)
try {
this._finalRoutes[route].apply(this, args);
}
catch (err) {
window.history.back(); //go back, error control...
}
else
window.history.back(); //no permissions, go back
},
evalPreconditions: function (preconds, args) {
if (!this.evaluateRoutesFn) {
throw "WARNING: Evaluate routes function must be overriden to evaluate them. " +
"Don't assign toEvaluate if you won't do it. The evaluation has been skipped.";
}
if (!preconds)
return true;
var evalResult = true;
for (var i = 0, len = preconds.length; i < len; i++) {
evalResult = this.evaluateRoutesFn(preconds[i], args);
if (!evalResult) {
throw "ERROR: The precondition is not truth.";
break;
}
}
return evalResult;
},
route: function (route, name, callback) {
this._finalRoutes[route] = (!callback) ? this[name] : callback;
var that = this;
callback = function (path) {
return function () {
var aps = Array.prototype.slice;
var args = aps.call(arguments);
args.unshift(path);
that.onRoutes.apply(that, args);
};
} (route);
return Backbone.Router.prototype.route.call(this, route, name, callback);
}
});
Backbone.ModuleRoute=Backbone.Router.extend({
evaluateRoutesFn:null,
toEvaluate:null,
构造函数:函数(前缀、选项){
这._finalRoutes={};
Backbone.subcure.prototype.constructor.call(this,prefix,options);
},
onRoutes:功能(路由){
var aps=Array.prototype.slice;
var args=aps.call(参数);
args.shift();
var routeName=this.routes[route];
var evalResult=真;
试一试{
var-toEval=this.toEvaluate&&this.toEvaluate[routeName];
如果(toEval){
evalResult=this.evalPreconditions(toEval.moduleReconditions,args);
//更多的未来先决条件
}
}
捕捉(错误){
evalResult=false;
}
if(evalResult)
试一试{
this.\u finalRoutes[route].apply(this,args);
}
捕捉(错误){
window.history.back();//返回,错误控制。。。
}
其他的
window.history.back();//没有权限,返回
},
evalPreconditions:功能(预编码、参数){
如果(!this.evaluateRoutesFn){
throw“警告:必须重写Evaluate routes函数才能对其进行评估。”+
“如果您不愿意,请不要分配给评估。评估已被跳过。”;
}
如果(!preconds)
返回true;
var evalResult=真;
对于(变量i=0,len=prectds.length;i