构建基于Javascript的移动web CMS——加载JSON文件
2014-07-25 08:30
330 查看
在上一篇中说到了如何创建一个Django Tastypie API给移动CMS用,接着我们似乎也应该有一个本地的配置文件用于一些简单的配置,如"获取API的URL"、"产品列表"、"SEO"(在一开始的时候发现这是不好的,后面又发现Google的爬虫可以运行Javascript,不过也是不推荐的。)这些东西是不太需要修改的,直接写在代码中似乎又不好,于是放到了一个叫作
font : Load web fonts using the WebFont Loader API (requires
goog : Load Google APIs asynchronously (requires
image : Load image files as dependencies. Option to "cache bust".
json : Load JSON files and parses the result. (Requires
mdown : Load Markdown files and parses into HTML. (Requires
noext : Load scripts without appending ".js" extension, useful for dynamic scripts.
于是,我们可以用到这里的json用来加载JSON文件,虽然也可以用Requirejs的text插件,但是这里的json有对此稍稍的优化。在后面的部分中我们也用到了mdown,用于显示一个md文件,用法上大致是一样的。
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
二、直接下载
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 and
propertyParser).
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
结束
获取代码
一、使用gitgit clone https://github.com/gmszone/moqi.mobi git checkout b03f54c二、直接下载
其它
CMS效果: 墨颀 CMSQQ讨论群: 344271543项目: https://github.com/gmszone/moqi.mobi相关文章推荐
- 构建基于Javascript的移动web CMS——模板
- 构建基于Javascript的移动web CMS入门——简介
- 构建基于Javascript的移动web CMS——Hello,World
- 构建基于Javascript的移动web CMS——模板
- 构建基于Javascript的移动web CMS——添加jQuery插件
- 构建基于Javascript的移动web CMS——Hello,World
- 构建基于Javascript的移动web CMS——整合Django
- 构建基于Javascript的移动web CMS——加入jQuery插件
- 构建基于Javascript的移动CMS——生成博客(二).路由
- 构建基于Javascript的移动CMS——生成博客(二).路由
- 构建基于Javascript的移动CMS——生成博客(一)
- 构建基于Javascript的移动CMS——加入滑动
- 构建基于Javascript的移动CMS——添加滑动
- 构建基于Javascript的移动CMS——生成博客(三).重构
- android webview 使用js加载json文件失败
- iOS 基于JavaScriptCore 不等webView加载完毕就交互,网页获取原生内容。 webView的高级用法之JS交互,js与oc的相互调用
- 基于MVC+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据
- 57.加载json文件 webview加载网页
- (转)基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据
- 基于Liferay的平台下,portlet在各个模式下分别加载以<footer-portlet-javascript>定义的js文件的不可行性 推荐