自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Titanium TiMVC之功能擴(kuò)展Redux

移動(dòng)開發(fā)
之前向大家介紹了關(guān)于TiMVC的使用,想必對(duì)此框架與有一定的認(rèn)識(shí)了,這次要介紹的是一個(gè)出色的第三方開源類庫在TiMVC里的使用 :smile: 。這個(gè)類庫就是 Redux。

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

關(guān)于如何將第三方類庫整合到TiMVC里,大家可看我之前發(fā)的文章:《TitaniumTiMVC之功能擴(kuò)展––添加第三方類庫的使用》。OK,當(dāng)一切設(shè)置好后,就先讓我們看看到底R(shí)edux類庫有什么好處吧。根據(jù)官方的文檔和使用說明,我們可以知道其主要有以下功能:

1.簡化UI的創(chuàng)建語法。如創(chuàng)建一個(gè)VieworWindow時(shí)候,不需要用到Ti.UI.createVieworTi.UI.createWindow這樣的長語法了,而可以直接使用簡單的語法進(jìn)行創(chuàng)建,如:

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

這種創(chuàng)建方式適合現(xiàn)有所有UI使用,而且就算有新的UI出來,你甚至也可以方便地自己添加進(jìn)去,這也是此類庫的一個(gè)特性,可自定義所有類庫的動(dòng)態(tài)創(chuàng)建語法。

2.簡化的事件綁定。在Ti官方語法中,要為一個(gè)按鈕添加事件,需要以下寫法:

  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'}); 

對(duì)于自定義事件的綁定,一般 Ti 里寫法是:

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

而使用Redux,則可以定義一次就不斷重復(fù)使用:

  1. $.fn.addEventBinder('myCustomEvent'); // 只需定義一次,就可以不斷重復(fù)使用了 
  2. $(button).myCustomEvent(function() { }); 

而使用Redux,則可以定義一次就不斷重復(fù)使用:

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

3.動(dòng)態(tài)設(shè)置UI控件的樣式。這個(gè)在Ti里本身是不支持的,使用Redux可以動(dòng)態(tài)為控件設(shè)置不同的樣式,如:

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

而且還可以按選擇器來設(shè)置控制的默認(rèn)樣式哦:

  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。這個(gè)格式語法基本上和JSS一樣,不過其使用起來可比JSS要方便多了!首先使用RJSS的話,所作的任何修改都不需要再重新clean一次項(xiàng)目就可以生效,這個(gè)是我最喜歡的,呵呵。其次你還可以直接在RJSS里填寫JS或者Ti的條件語句!

一般的語法就不用說了,和JSS一樣,支持直接使用Ti里原對(duì)象與方法等,比較特別的用法是可以添加條件屬性到每個(gè)選擇器里,如要為不同的平臺(tái)使用同一個(gè)選擇器,可以這樣寫:

 

  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.  

 

這樣寫的好處是顯而易見的,就是同一個(gè)className只需在代碼里指定一次,判斷的邏輯就直接在rjss文件里完成了,讓前臺(tái)代碼更靈活,代碼也簡潔些了。另外就是rjss還支持直接使用控制名稱的樣式設(shè)置,如要為所有vieworwindow設(shè)置統(tǒng)一的樣式,只需直接寫:

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

這樣對(duì)全局樣式的控制也方便很多啦:biggrin:。哦,最后差點(diǎn)忘了說如何調(diào)用rjss文件了,Redux提供專門的方法引用rjss文件,而且還可以定義全局和局部的rjss文件,只需如下引用即可:

includeRJSS('common.rjss');

includeRJSSGlobal('common.rjss');

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

最后要說一下的,如何能讓Redux更好地在TiMVC里使用,只是添加到了config里還不夠的,為了將Redux運(yùn)用到全局框架中,還必須在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,請按以下步驟創(chuàng)建:1.在mvc目錄里創(chuàng)建一個(gè)style目錄,然后可以按需要?jiǎng)?chuàng)建不同平臺(tái)的目錄名稱,如android,iphone,ipad等幾個(gè)目錄2.在style目錄里創(chuàng)建一個(gè)global.rjss文件,這個(gè)就是全局的樣式文件3.在不同平臺(tái)目錄下,根據(jù)你的view名稱創(chuàng)建同名的rjss文件,這樣做是可以獨(dú)立為某個(gè)view設(shè)置其樣式,如你一個(gè)view名稱為home的,那么就可以在iphone目錄下創(chuàng)建名為home.rjss的樣式文件,當(dāng)然其他平臺(tái)目錄下也可創(chuàng)建一個(gè)同名文件以應(yīng)用到不同平臺(tái)的樣式效果。4.在config里創(chuàng)建stylePath和customStyle(不根據(jù)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里調(diào)用loadGlobalStyle:

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

5.在components/controller.js文件里添加各平臺(tái)下的rjss文件的引用,因?yàn)橐鶕?jù)不同view動(dòng)態(tài)添加,所以代碼可寫到this.renderWithLayout方法里,如下:

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

這樣就可以自動(dòng)加載不同view下的樣式文件了,同時(shí)你還可以自添加定義樣式文件,這個(gè)自定義樣式文件不需與view同名,也就是說可為不同平臺(tái)添加一個(gè)全局樣式文件:biggrin:

責(zé)任編輯:佚名 來源: coderblog.in
相關(guān)推薦

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

分布式數(shù)據(jù)接口同步機(jī)制

2014-01-23 16:24:09

網(wǎng)易郵箱

2012-05-18 11:34:03

Titaniumcons

2012-05-18 11:29:55

Titaniumpros

2012-02-13 14:41:50

Titanium架構(gòu)分析

2011-06-10 13:42:50

QT mplayer 播放器

2016-10-31 11:26:13

ReactRedux前端應(yīng)用

2012-06-26 10:40:43

Titanium

2012-04-20 11:07:12

Titanium

2009-11-25 10:43:57

GoogleChrome擴(kuò)展功能

2012-05-18 10:08:56

TitaniumAndroid

2012-04-19 16:22:12

TitaniumTabGroup

2012-05-23 09:41:37

Titanium St卸載

2012-04-19 12:58:26

TitaniumJSS

2012-06-14 09:42:20

跨平臺(tái)工具AppceleratoTitanium
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)