Titanium TiMVC之功能扩展Redux

移动开发
之前向大家介绍了关于TiMVC的使用,想必对此框架与有一定的认识了,这次要介绍的是一个出色的第三方开源类库在TiMVC里的使用 :smile: 。这个类库就是 Redux。

Redux的官方下载地址为:https://github.com/dawsontoth/Appcelerator-Titanium-Redux

关于如何将第三方类库整合到TiMVC里,大家可看我之前发的文章:《TitaniumTiMVC之功能扩展––添加第三方类库的使用》。OK,当一切设置好后,就先让我们看看到底Redux类库有什么好处吧。根据官方的文档和使用说明,我们可以知道其主要有以下功能:

1.简化UI的创建语法。如创建一个VieworWindow时候,不需要用到Ti.UI.createVieworTi.UI.createWindow这样的长语法了,而可以直接使用简单的语法进行创建,如:

  1. var view = View({className:'view'}); 

这种创建方式适合现有所有UI使用,而且就算有新的UI出来,你甚至也可以方便地自己添加进去,这也是此类库的一个特性,可自定义所有类库的动态创建语法。

2.简化的事件绑定。在Ti官方语法中,要为一个按钮添加事件,需要以下写法:

  1. button.addEventListener('click', function() { alert('clicked!'); }); 
  2. button.fireEvent('click'); 
  3. button.fireEvent('click', {src: 'me'}); 

 而使用了Redux后,只需像jQuery的语法来绑定即可:

  1. $(button).click(function() { alert('clicked!'); }) .click() .click({src: 'me'}); 

对于自定义事件的绑定,一般 Ti 里写法是:

  1. button.addEventListener('myCustomEvent', function() { }); 

而使用Redux,则可以定义一次就不断重复使用:

  1. $.fn.addEventBinder('myCustomEvent'); // 只需定义一次,就可以不断重复使用了 
  2. $(button).myCustomEvent(function() { }); 

而使用Redux,则可以定义一次就不断重复使用:

  1. Label.setDefault({ font: {fontWeight: 'bold' } }); 
  2. var label = new Label(); 
  3. alert(label.font.fontWeight == 'bold'); 

3.动态设置UI控件的样式。这个在Ti里本身是不支持的,使用Redux可以动态为控件设置不同的样式,如:

  1. Label.setDefault({font:{fontWeight:'bold'}}); 
  2.  
  3. varlabel=newLabel(); 
  4.  
  5. alert(label.font.fontWeight=='bold'); 

而且还可以按选择器来设置控制的默认样式哦:

  1. $.fn.setDefault('#myID',{text:'Hello,World!',color:'red'}); 
  2.  
  3. $.fn.setDefault('.myClassName',{font:{fontSize:12},color:'green'}); 
  4.  
  5. varlabel=newLabel({id:'myID',className:'myClass',color:'blue'}); 
  6.  
  7. alert(label.text=='Hello,World!');alert(label.color=='blue'); 

怎样?挺酷吧?呵呵,除此之外,还有更酷的,就是接下来要说到的RJSS格式的支持。

4.Redux特有的样式格式--RJSS。这个格式语法基本上和JSS一样,不过其使用起来可比JSS要方便多了!首先使用RJSS的话,所作的任何修改都不需要再重新clean一次项目就可以生效,这个是我最喜欢的,呵呵。其次你还可以直接在RJSS里填写JS或者Ti的条件语句!

一般的语法就不用说了,和JSS一样,支持直接使用Ti里原对象与方法等,比较特别的用法是可以添加条件属性到每个选择器里,如要为不同的平台使用同一个选择器,可以这样写:

 

  1. [Ti.Platform.osname="android"
  2.  
  3.  
  4. .mainWin{ 
  5.  
  6. top:'20dp', 
  7.  
  8. bottom:'30dp', 
  9.  
  10. right:'10dp', 
  11.  
  12. left:'30dp', 
  13.  
  14. width:'200dp', 
  15.  
  16. height:'400dp' 
  17.  
  18.  
  19.  
  20. [Ti.Platform.osname!="android"] 
  21.  
  22.  
  23. .mainWin 
  24.  
  25.  
  26. top:'10', 
  27.  
  28. bottom:'10', 
  29.  
  30. right:'5', 
  31.  
  32. left:'10', 
  33.  
  34. width:'300', 
  35.  
  36. height:'500' 
  37.  
  38.  

 

这样写的好处是显而易见的,就是同一个className只需在代码里指定一次,判断的逻辑就直接在rjss文件里完成了,让前台代码更灵活,代码也简洁些了。另外就是rjss还支持直接使用控制名称的样式设置,如要为所有vieworwindow设置统一的样式,只需直接写:

  1. View 
  2.  
  3.  
  4. width:200 
  5.  
  6.  
  7. Window 
  8.  
  9.  
  10. backgroundColor:'#fff' 
  11.  

这样对全局样式的控制也方便很多啦:biggrin:。哦,最后差点忘了说如何调用rjss文件了,Redux提供专门的方法引用rjss文件,而且还可以定义全局和局部的rjss文件,只需如下引用即可:

includeRJSS('common.rjss');

includeRJSSGlobal('common.rjss');

是不是很方便呢?哈哈~!

最后要说一下的,如何能让Redux更好地在TiMVC里使用,只是添加到了config里还不够的,为了将Redux运用到全局框架中,还必须在core/timvc.js文件里的this.includeBaseComponents方法里添加引用:

  1. this.includeBaseComponents=function() 
  2.  
  3.  
  4. //这里添加引用到全局 
  5.  
  6. Titanium.include 
  7.  
  8. (self.config.resDir+self.config.vendorPath+"redux.js"); 
  9.  
  10. ... 
  11.  
  12. } 

如果要使用rjss,请按以下步骤创建:1.在mvc目录里创建一个style目录,然后可以按需要创建不同平台的目录名称,如android,iphone,ipad等几个目录2.在style目录里创建一个global.rjss文件,这个就是全局的样式文件3.在不同平台目录下,根据你的view名称创建同名的rjss文件,这样做是可以独立为某个view设置其样式,如你一个view名称为home的,那么就可以在iphone目录下创建名为home.rjss的样式文件,当然其他平台目录下也可创建一个同名文件以应用到不同平台的样式效果。4.在config里创建stylePath和customStyle(不根据view名称的自定义样式文件)的配置:

  1. /** 
  2. * Style folder path 
  3. * @type String 
  4. */ 
  5. stylePath : "mvc/style/", 
  6. /** 
  7. * Custom style in the device folder, another style file will same with the view's name 
  8. * @type array 
  9. */ 
  10. customStyle : [{ 
  11. name : "main" 
  12. }], 

4.在core/timvc.js里添加以下代码以引用rjss文件:

  1. /** 
  2. * Load style 
  3. * @param {String} name vendor name based on vendor path in config (usally /mvc/style) 
  4. * @returns boolean with load status 
  5. */ 
  6. this.loadStyle = function(name) { 
  7. var path = self.config.resDir + self.config.stylePath + Ti.Platform.osname + "/" + name + ".rjss"
  8. try { 
  9. if(Titanium.Filesystem.getFile(path).exists()) { 
  10. includeRJSS(path); 
  11. return true
  12. return false
  13. catch(e) { 
  14. self.notice('Cannot Include Style File: ' + path); 
  15. self.debug(path); 
  16. return false
  17.  
  18. /** 
  19. * Load global and coustom style 
  20. * @param {String} name vendor name based on vendor path in config (usally /mvc/style) 
  21. * @returns boolean with load status 
  22. */ 
  23. this.loadGlobalStyle = function() { 
  24. var path = self.config.resDir + self.config.stylePath + "global.rjss"
  25. try { 
  26. includeRJSSGlobal(path); 
  27. for(var i = 0, v = self.config.customStyle.length; i < v; i++) { 
  28. self.loadStyle(self.config.customStyle[i].name); 
  29. return true
  30. catch(e) { 
  31. self.sysDebug(path + '\r\n' + e); 
  32. self.notice('Cannot Include Style File: ' + path); 
  33. return false

然后在初始化方法this._init里调用loadGlobalStyle:

  1. <pre>this._init = function() { 
  2. self.includeBaseComponents(); 
  3. self.loadGlobalStyle();//一定要在includeBaseComponents下面哦 
  4. ... 

5.在components/controller.js文件里添加各平台下的rjss文件的引用,因为要根据不同view动态添加,所以代码可写到this.renderWithLayout方法里,如下:

  1. this.render = function(view,data){ 
  2. //引用不同平台的 
  3. self.App.loadStyle(view); 
  4. ... 

这样就可以自动加载不同view下的样式文件了,同时你还可以自添加定义样式文件,这个自定义样式文件不需与view同名,也就是说可为不同平台添加一个全局样式文件:biggrin:

责任编辑:佚名 来源: coderblog.in
相关推荐

2012-04-19 14:16:22

TitaniumTiMVC

2012-04-19 13:55:19

TitaniumTiMVC

2013-07-22 17:59:14

VMwarevSphere

2010-07-19 12:47:04

SQL Server

2022-04-08 11:08:17

分布式数据接口同步机制

2014-01-23 16:24:09

网易邮箱

2012-05-18 11:29:55

Titaniumpros

2012-05-18 11:34:03

Titaniumcons

2012-02-13 14:41:50

Titanium架构分析

2011-06-10 13:42:50

QT mplayer 播放器

2016-10-31 11:26:13

ReactRedux前端应用

2012-04-20 11:07:12

Titanium

2012-06-26 10:40:43

Titanium

2009-11-25 10:43:57

GoogleChrome扩展功能

2012-05-17 09:09:05

Titanium单元测试

2012-04-19 12:58:26

TitaniumJSS

2012-06-14 09:42:20

跨平台工具AppceleratoTitanium

2012-05-18 10:08:56

TitaniumAndroid

2012-04-19 16:22:12

TitaniumTabGroup

2012-05-23 09:41:37

Titanium St卸载
点赞
收藏

51CTO技术栈公众号