日期:2014-05-16 浏览次数:20436 次
dbo.users = function(){ var users = pure.db.get("users"); return { list: function() { if (!users.count()) { init(); } return users.list().$sort({ name: 1 }); }, save: function(user) { var q = { name: user.name }; users.update(q, user, true, false); }, remove: function(name) { users.remove({ name: name }); }, exists: function(name) { return users.count({ name: name }) > 0; } } function init() { pure.db.eval(function(){ var users = db.users; users.drop(); users.ensureIndex({ name: 1 }); users.insert({ name: "user1", desc: "desc1" }); users.insert({ name: "user2", desc: "desc2" }); users.insert({ name: "user3", desc: "desc3" }); }); } }();
var views = {}; views.index = function($, users) { $(users).each(function(i) { this.index = i; }); var content = $("#content-tmpl").tmpl(), rows = $("#user-tmpl").tmpl(users); $("#content").html(content); $("#content tr:first").after(rows); };
(function() { var log = pure.log("proxy.security"); proxy.security = { priority: 80 }; proxy.security.page = { priority: 100, expr: /^page./, func: function(name, method, args) { var req = args[1]; var role = req.session.getAttribute("user.role") + ""; if (role === "admin") { return this[method].apply(this, args); } else { log.info("Redirect to login page."); return pure.render("login"); } } } proxy.security.api = { ... } }());
page.index = function(params, req, res) { var users = pure.proxy("dbo.users").list(), data = { users: JSON.stringify(users) }; return pure.render("index", function($){ views.index($, users); $("#data").html($("#data-tmp