开篇扯淡距离上一篇文章已经有好几个月,也不是没有时间记录点东西,主要是换了新的工作,在一家外资工作,目前的工作内容大多都是前端开发,新接触的东西因为时间原因,大多还不成体系,所以这么长时间什么都没记录下来,也正是因为新的工作内容,才有了今天这篇文章。
这篇文章是我自己的博客项目的前端重写,因为目前asp.netAPI和单页应用的流行,结合目前工作中用到的东西,我决定把我的博客项目的前端部分整个重写,(以前的就是一坨…)
步入正题背景知识RequireJShttp://www.requirejs.org/
Knockouthttp://knockoutjs.com/
BootStraphttp://getbootstrap.com/
PubSubJShttps://github.com/mroderick/PubSubJS
如果没有接触过的朋友请自行谷歌百度吧,就不浪费口水,额,键盘啦,什么,没有jQuery,呵呵,呵呵,正如Knockout官方文档里说的,Everyoue loves jquery。
RequireJS我用来做模块加载器,Knockout做MVVM分离也是爽到没朋友(谁用谁知道),Bootstrap搭建界面布局,PubSub,看着名字就知道啦。
文档结构
Libs:放置上文中提到的各种框架和工具;
App:主要的工作目录,articleList、catalog、articleViewer分别代表整个前端应用中的一个组件,对应的.html文件是他们自身的视图模板;
Utilities:存放一些工具类,如检测设备、格式化Url和字符串等;
Layout:只有一个文件,存放了整个前端应用的模板,可以通过更改这个文件,来改变各个组件的表现形式。
服务端API准备为这个示例,我只准备了三个服务端API:
GetCatalog 得到文章类型目录:
namespaceMiaoBlog.Controllers.API
{
public classCatalogController:ApiController
{
publicIEnumerable<CategoryView> Get()
{
GetAllCategoriesResponse response = articleCatalogService.GetAllCategories();
returnresponse.Categories;
}
}
}
GetArticlesByCategory 根据类型ID和页面编号获取文章目录,
GetArticle 根据文章ID获取文章内容等信息:
namespaceMiaoBlog.Controllers.API
{
public classArticlesController:ApiController
{
publicIEnumerable<ArticleSummaryView> GetArticlesByCategory(intcategoryId,intpageNumber)
{
GetArticlesByCategoryRequest request = GenerateArticlesByCategoryRequestFrom(categoryId, pageNumber-1);
GetArticlesByCategoryResponse response = articleCatalogService.GetArticlesByCategory(request);
returnresponse.Articles;
}
publicArticleDetailPageView GetArticle(intid)
{
ArticleDetailPageView detailView =newArticleDetailPageView();
GetArticleRequest request =newGetArticleRequest() { ArticleId = id };
GetArticleResponse response = articleCatalogService.GetArticle(request);
ArticleView articleView = response.Article;
detailView.Article = articleView;
detailView.Comments = response.Comments;
detailView.Tags = response.Tags;
returndetailView;
}
PRivate staticGetArticlesByCategoryRequest GenerateArticlesByCategoryRequestFrom(intcategoryId,intindex)
{
GetArticlesByCategoryRequest request =newGetArticlesByCategoryRequest();
request.NumberOfResultsPerPage =int.Parse(applicationSettingsFactory.GetApplicationSettings().NumberOfResultsPerPage);
request.Index = index;
request.CategoryId = categoryId;
request.ExcerptLength =int.Parse(ApplicationSettingsFactory.GetApplicationSettings().ExcerptLength);
returnrequest;
}
}
}
Require配置与系统配置这里我用到的Require的几个常用插件:domReady、CSS、text.
paths配置了引用的js的别称:
paths:{
'lib/jquery':'./Libs/jquery-1.11.1',
'lib/underscore':'./Libs/underscore',
'lib/unserscore/string':'./Libs/underscore.min',
'lib/backbone':'./Libs/backbone',
'lib/backbone/eproxy':'./Libs/backbone.eproxy',
'lib/backbone/super':'./Libs/backbone.super',
'lib/pubsub':'./Libs/pubsub',
'r/css':'./Libs/css',
'r/less':'./Libs/less',
'r/text':'./Libs/text',
'r/domReady':'./Libs/domReady',
'r/normailize':'./Libs/normalize',
'pubsub':'./Libs/pubsub',
'lib/ko':'./Libs/knockout-3.2.0',
'utility':'./Utilities/utility',
'util/matrix2d':'./Utilities/matrix2d',
'util/meld':'./Utilities/meld',
'lib/bootstrap':'./Libs/bootstrap-3.2.0/dist/js/bootstrap',
'lib/bootstrap/css':'./Libs/bootstrap-3.2.0/dist/css/'
},
shim的配置略过;
然后就是require的调用入口了,从这里启动整个前端应用:
require(['lib/jquery','r/domReady','lib/underscore','config','r/text!Layout/template.html','r/css!lib/bootstrap/css/bootstrap.css','lib/bootstrap', ],function($, domReady, _, config, template) {
domReady(function() {
varrootContainer = $("body").find("[data-container='root']");
varoTemplate=$(template);
varmodules = $("[data-module]",oTemplate);
_.each(modules,function(module, index) {
require(["App/"+ $(module).attr("data-module")],function(ModuleClass) {
varcombineConfig = _.defaults(config[$(module).attr("data-module")], config.application);
varoModule =newModuleClass(combineConfig);
oModule.load();
oModule.render(modules[index]);
});
});
rootContainer.append(oTemplate);
});
});
这里看到了template.html通过r/text引入,上文中提到过,它就是整个应用程序的模板文件,先看一下它的结构我再接着解释代码内容:
<div class="container">
<div class="row">
<div class="col-lg-3" data-module="catalog"></div>
<div class="col-lg-9" data-module="articleList"></div>
</div>
<div data-module="articleViewer"></div>
</div>
这样看起来,代码的意图就明晰多了,在页面中查到了data-container为root的节点,将它作为整个前端应用的根节点,然后再读取上面的模板文档,根据模板中标签的data-module属性,获得模块名称,然后动态的加载模块。
在这里我使用了Underscore的_.defaults方法,给各个模块取得了各自的配置内容和公用配置内容,Underscore是js的一个工具类,自行百度,不多介绍,还有个个人推荐的Underscore.string,它提供了很多js处理字符串的方法,比较方便好用。
上文所提及的应用配置文件如下:
define(function() {
return{
application: {
Events: {
SWITCH_CATEGORY:"Miaoblog_Switch_Category",
OPEN_ARTICLE:"Miaoblog_Open_Article"
}
},
catalog: {
APIs: {
GetCatalog: {
Url:"http://localhost:15482/api/Catalog"
}
}
},
articleList: {
APIs: {
GetArticleList: {
Url:"http://localhost:15482/api/Articles",
ParamsFormat:"categoryId={0}&pageNumber={1}"
}
}
},
articleViewer: {
APIs: {
GetArticle: {
Url:"http://localhost:15482/api/Articles/{0}"
}
}
}
};
});
结合上文中的代码,可以明确的知道一点,各个组件模块最终只会得到关于它自己的配置项目和公用的,也就是application级别的配置内容,在application对象中的Events对象在下文中将会做详细的介绍。
模块中的工作就已catalog模块为例,先贴上代码,再做解释:
/// <reference path="../Libs/require.js" />
define(['lib/jquery','lib/ko','lib/underscore','pubsub','r/text!App/catalogList.html'],
function($, ko,_, hub,template) {
varapp =function(config) {
this.catalogList =null;
this.oTemplate = $(template);
this.config = config;
}
_.extend(app.prototype, {
load:function() {
varself =this;
$.Ajax({
type:"GET",
async:false,
url: self.config.APIs.GetCatalog.Url,
dataType:"json",
success:function(data) {
self.catalogList = data;
},
error:function(jqXHR, textStatus, error) {
console.log(error);
}
});
},
render:function(container) {
varself =this;
varoContainer = $(container);
varlist = {
categories: ko.observableArray(),
switchCategory:function(selected) {
//alert("Hello world"+selected.Id);
hub.publish(self.config.Events.SWITCH_CATEGORY, selected.Id);
}
};
list.categories(self.catalogList);
oContainer.append(this.oTemplate);
ko.applyBindings(list,this.oTemplate.get(0));
}
});
returnapp;
});
这里唯一新的内容就是大杀器knockout终于出场了。
从上一节内容可以看到,主模块将会一次调用子模块的load和render方法,在这个子模块catalog中,load阶段,通过对服务端的api调用得到了文章目录,API的地址是通过config文件的解析传递过来的,的数据结构是这样的:
而在render阶段,传入的参数为仅供给当前组件的占位,组件自身可以决定怎样去布局这个占位,这就涉及到了它自身的模板文件了:
<ulclass="nav nav-pills nav-stacked"data-bind="foreach:categories">
<li>
<a href="#"data-bind="attr:{categoryId:Id},click:$parent.switchCategory">
<!--ko text: Name--><!--/ko-->
<spanclass="badge pull-right"data-bind="text:ArticlesCount"></span>
</a>
</li>
</ul>
在数据和视图两者间,我使用了Knockout进行绑定,它的优势在文档中有详细的描述,如果您想了解的话,就在文章开始找链接吧;
接着分析代码,在视图中,使用了Bootstrap的样式创建了一个目录样式,并且banding了一个switchCategory方法到viewModel中,当我们点击每一个类型链接时候,系统会通过上文中提到的Pubsub工具发布一个SWITCH_CATEGORY的事件出去,并且携带了所点击类型的ID,这个常量字符串也是在上一节中的config文件中配置的。
模块间的工作上一节中提到了Pubsub发布了一个事件出去,意图是希望文章列表或者其他什么关心这个事件的组件去做它自己的工作,在这个示例中当然就只有articleList这个组件了,来看一下这个组件的代码:
/// <reference path="../Libs/require.js" />
define(['lib/jquery','lib/ko','lib/underscore','utility','pubsub','r/text!App/articleList.html','r/css!App/CommonStyle/style.css'],
function($, ko, _, utility,hub,layout) {
varapp =function(config) {
this.config = config;
this.oTemplate = $(layout);
this.currentPageArticles =null;
this.currentCategoryId =null;
this.currentPageNumber =null;
this.articleListViewModel =null;
}
_.extend(app.prototype, {
initialize:function(){
},
load:function() {
varself =this;
hub.subscribe(this.config.Events.SWITCH_CATEGORY,function(msg, data) {
self.switchCategory(data);
});
},
render:function(container) {
varself =this;
varoContainer = $(container);
this.articleListViewModel = {
articles: ko.observableArray(),
openArticle:function(selected) {
hub.publish(self.config.Events.OPEN_ARTICLE, selected.Id);
}
};
oContainer.append(this.oTemplate);
ko.applyBindings(this.articleListViewModel,this.oTemplate.get(0));
},
switchCategory:function(categoryId) {
varself =this;
self.currentCategoryId = categoryId;
self.currentPageNumber = 1;
$.ajax({
type:"GET",
async:true,
url: utility.FormatUrl(false,self.config.APIs.GetArticleList,categoryId,self.currentPageNumber),
dataType:"json",
success:function(data) {
self.articleListViewModel.articles(data);
}
});
},
turnPage:function(pageNumber) {
}
});
returnapp;
}
);
这里主要看以下两个点:
1.在Load阶段,组件监听了SWITH_CATEGORY这个事件,在事件触发后,将调用switchCategory方法;因为这个SWITCH_CATEGORY这个常量是配置在application对象中,所以它在各个组件间是公用的;
2.在switchCategory中,传入的即使上一节中提到的类型ID,然后同样通过上一节的方法,调用服务端API,获得数据,然后使用knockout进行数据绑定,在ViewModel中,可以看到一个openArticle方法,同样发布了一个事件,在这个示例中,是右articleViewer监听的,由于原理相近,就不多做解释了,仅有破图了代码送上。
烂图赏鉴
代码送上,仅供吐槽onedrive就不用了,虽然很搞到上,但是谁知道哪天就又…你懂的
百度网盘地址:http://pan.baidu.com/s/1o6meoKa