Skip to content

Instantly share code, notes, and snippets.

@hiyangguo
Created June 17, 2016 03:18
Show Gist options
  • Save hiyangguo/08c3240531d91cc0e5bd48fe6d0d8cbc to your computer and use it in GitHub Desktop.
Save hiyangguo/08c3240531d91cc0e5bd48fe6d0d8cbc to your computer and use it in GitHub Desktop.
Basic temp example
/*
type config = {
args: {
Query参数名称 : 预定义类型 或 Converter
}
}
type 预定义类型 = 'Int' | 'String'
type Converter = function(queryParamValue):Promise
*/
function page(config,init) {
return function (contextInfo,pageOp) {
var predefinedTypes = { Int: isInt };
var args = config.args, promiseList = [];
var argMap = {};
for (var name in args) {
var value = url.get(name);
var converter = args[name];
if (typeof converter === 'string') {
converter = predefinedTypes[converter];
}
var p = converter(value);
promiseList.push(p);
(function (name) {
p.done(function (value) {
argMap[name] = value;
});
})(name);
}
$.when(promiseList).done(function () {
init(contextInfo,pageOp,argMap);
}).fail(function () {
alert('Bad Request');
});
};
}
function isInt(value) {
var d = $.Deferred();
if (+value === parseInt(value)) d.resolve(+value);
else d.reject();
return d.promise();
}
// example
// company/edit?id=1
model.get = function (id) {
// jquery ajax方法本来返回promise对象多方便啊
return $.get('/api/company/'+id);
}
app.page.edit = page({
args:{
id:function (value) { return isInt(value).then(model.get) }
}
},function (contextInfo,pageOp,argMap) {
var company = argMap.id;
alert(company.name); // company 已经过model.get返回对象了
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment