在上一篇中说到了如何创建一个Django Tastypie API给移动CMS用,接着我们似乎也应该有一个本地的配置文件用于一些简单的配置,如"获取API的URL"、"产品列表"、"SEO"(在一开始的时候发现这是不好的,后面又发现Google的爬虫可以运行Javascript,不过也是不推荐的。)这些东西是不太需要修改的,直接写在代码中似乎又不好,于是放到了一个叫作configure.json
的文件里。
RequireJS Plugins
网上搜索到一个叫RequireJS Plugins的repo。
里面有这样的几个插件:
- async : Useful for JSONP and asynchronous dependencies (e.g. Google Maps).
- font : Load web fonts using the WebFont Loader API (requires
propertyParser
) - goog : Load Google APIs asynchronously (requires
async!
plugin andpropertyParser
). - image : Load image files as dependencies. Option to "cache bust".
- json : Load JSON files and parses the result. (Requires
text!
plugin). - mdown : Load Markdown files and parses into HTML. (Requires
text!
plugin and a markdown converter). - noext : Load scripts without appending ".js" extension, useful for dynamic scripts.
于是,我们可以用到这里的json用来加载JSON文件,虽然也可以用Requirejs的text插件,但是这里的json有对此稍稍的优化。
在后面的部分中我们也用到了mdown,用于显示一个md文件,用法上大致是一样的。
RequireJS JSON文件加载
将json.js插件放到目录里,再配置好main.js。
require.config({
paths: {
'text': 'text',
jquery: 'jquery',
json: 'require/json'
},
shim: {
underscore: {
exports: '_'
}
}
});
require(['app'], function(App) {
App.initialize();
});
于是我们将HomeView.js中的data变为configure的数据,这样便可以直接使用这个json文件。
define([
'jquery',
'underscore',
'mustache',
'text!/index.html',
'json!/configure.json'
], function($, _, Mustache, indexTemplate, configure) {
var HomeView = Backbone.View.extend({
el: $('#aboutArea'),
render: function() {
this.$el.html(Mustache.to_html(indexTemplate, configure));
}
});
return HomeView;
});
configure.json的代码如下所示:
{
"project": "My Sample Project"
}
最后实现的效果和模板结束是一样的,只会在页面上显示
My Sample Project
结束
获取代码
一、使用git
git clone https://github.com/gmszone/moqi.mobi
git checkout b03f54c
二、直接下载
其它
CMS效果: 墨颀 CMS
QQ讨论群: 344271543