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

Clouda API使用手冊(cè)之Router Model Collection

移動(dòng)開發(fā) Android
Clouda是簡(jiǎn)單,可依賴的實(shí)時(shí)Javascript框架。對(duì)一個(gè)想開發(fā)移動(dòng)webapp的開發(fā)者來(lái)說(shuō),可以使用clouda開發(fā)框架,實(shí)現(xiàn)一個(gè)功能和體驗(yàn)與native app齊平的輕應(yīng)用。

Router

用于建立URL(其路徑部分)和Controller的對(duì)應(yīng)關(guān)系,一個(gè)Controller可以對(duì)應(yīng)多個(gè)URL,但是一個(gè)URL只能對(duì)應(yīng)一個(gè)Controller。

  • add

    1. 語(yǔ)法:add({pattren:'', action:''}) 
    2.  
    3. 在router中添加一組pattren與Controller的對(duì)于關(guān)系 
    4.  
    5. sumeru.router.add( 
    6.     { 
    7.         pattern: '/studentList'
    8.         action: 'App.studentList' 
    9.     } 
    10. ); 

    • pattern

      URL(其路徑部分的值)

    • action

      對(duì)應(yīng)Controller的名稱

    如果你想關(guān)閉Server渲染,可使用下面方法:

    1. sumeru.router.add( 
    2.     { 
    3.         pattern: '/studentList'
    4.         action: 'App.studentList' 
    5.         server_render:false 
    6.     } 
    • server_render

      Server渲染開關(guān),false:關(guān)閉,默認(rèn)為開啟

  • setDefault

    語(yǔ)法:setDefault(controllerName)

    設(shè)置默認(rèn)啟動(dòng)Controller

    sumeru.router.setDefault('App.studentList');
  • externalProcessor.add(processor);

    語(yǔ)法:sumeru.router.externalProcessor.add(processor);

    添加外部處理器

    添加一個(gè)backbone的外部處理器  sumeru.router.externalProcessor.add(Backbone.Router.extend());

#p#

Model

Model用來(lái)定義App的數(shù)據(jù)模型。

  1. Model.student = function(exports){ 
  2.     exports.config = { 
  3.         fields: [ 
  4.             {name : 'studentName', type: 'string'}, 
  5.             {name : 'age',         type: 'int'}, 
  6.             {name : 'gender',      type: 'string'
  7.         ] 
  8.     }; 
  9.  }; 
屬性
  • name

    字段的名稱

  • type

    字段的數(shù)據(jù)類型,包括一下數(shù)據(jù)類型:

類型 意義
int 整形
datetime 日期
string 字符串?dāng)?shù)
object 對(duì)象
array 數(shù)組
model 數(shù)據(jù)模型
collection 數(shù)據(jù)集合
  • relation

    使用relation時(shí)type屬性值必須為“model”。

    {name: 'class',  type: 'model', relation: 'one' , model:'Model.class'},
    • one

      引用一個(gè)Model

    • many

      引入一個(gè)Collection

  • defaultValue

    字段的默認(rèn)值

    {name: 'gender',  type: 'string', defaultValue:'male'},
  • validation

    {name: 'name',  type: 'string', validation:'length[1,20]'},

    字段的驗(yàn)證,validation包括以下方法:

方法 意義
length[min,max] 字段值的長(zhǎng)度在min-max的范圍。
minlength(min) 字段值不小于min
maxlength(min) 字段值不大于min
required 字段值不能為空
unique 字段值必須唯一
telephone 字段值必須為電話號(hào)碼格式
mobilephone 字段值必須為手機(jī)號(hào)碼格式,長(zhǎng)度為11位且必須為數(shù)字
email 字段值必須為email格式
onlyletter 字段值必須是字母
nospecialchars 字段值不能包含特殊字符
date 字段值必須是日期格式
url 字段值必須是URL
chinese 字段值必須是中文

注:多個(gè)驗(yàn)證條件之間使用" | "連接

    {name: 'name',  type: 'string', validation:'length[1,20]|required'},
  • addRule

    除了上面的驗(yàn)證方法外,還可以自定義驗(yàn)證方法。

    1. sumeru.validation.addRule(ruleName,{ 
    2.                                 "runat" : "client"
    3.  
    4.                                 驗(yàn)證方法  , 
    5.  
    6.                                 "msg"   : ""
    7.                                }); 
    • ruleName

      驗(yàn)證方法的名稱,如"chinese"、"url"

    • runat

      定義在哪個(gè)端上(client/server)進(jìn)行驗(yàn)證

      • client

        在客戶端上進(jìn)行驗(yàn)證

      • server

        在服務(wù)器端進(jìn)行驗(yàn)證

      • both

        兩段都需要驗(yàn)證

    • 驗(yàn)證方法:該API中框架提供三種自定義驗(yàn)證方法(三種方法(regxp/func/asyncFunc)每次只能使用一種

      • regxp

        使用自定義正則表達(dá)式對(duì)字段進(jìn)行驗(yàn)證

        1. sumeru.validation.addRule(ruleName,{ 
        2.                                        "runat" : "client"
        3.  
        4.                                        "regxp" : "()"
        5.  
        6.                                        "msg"   : ""
        7.                                }); 
      • func

        使用自定義函數(shù)對(duì)字段進(jìn)行驗(yàn)證

        1. sumeru.validation.addRule(ruleName,{ 
        2.                                          "runat" : "client"
        3.  
        4.                                          "func" : function(){}, 
        5.  
        6.                                          "msg"   : ""
        7.                                 }); 
      • asyncFunc

        該驗(yàn)證函數(shù)在服務(wù)器端運(yùn)行,先獲取指定modelObj的數(shù)據(jù),然后根據(jù)asyncFunc中的方法進(jìn)行驗(yàn)證,在callback中給出驗(yàn)證的結(jié)果。

        1. sumeru.validation.addRule(ruleName,{ 
        2.                                        "runat" : "client"
        3.  
        4.                                        "asyncFunc":function(callback,k,v,modelObj){} 
        5.  
        6.                                        "msg"   : ""
        7.                                }); 
    • msg

      驗(yàn)證失敗后返回的信息

  • create

    語(yǔ)法:create(modelName)

    創(chuàng)建一個(gè)model

    var newStudent = sumeru.model.create('Model.student')
  • setter

    newStudent.studentName = 'John';        
  • set

    語(yǔ)法:set(key,value)

    設(shè)置Model中相應(yīng)字段的值

    newStudent.set('studentName','John');
  • setData

    語(yǔ)法:setData(dataMap)

    使用dataMap對(duì)Model賦值

    1. newStudent.setData({'studnetName' : 'Smith'
    2.                             'age' : 19, 
    3.                          'gender' : 'male' 
    4.                   }); 
  • getter

    var name = newStudent.studentName;
  • get

    語(yǔ)法:get(key)

    獲取某一字段的值

    newStudent.get('studentName');
  • getId

    語(yǔ)法:getId()

    獲取model的唯一Id

    newStudent.getId();
  • getData

    語(yǔ)法:getData()

    返回一個(gè)JSON數(shù)據(jù)對(duì)象

    newStudent.getData();
  • destroy

    語(yǔ)法:destroy()

    刪除model

    newStudent.destroy();
  • onValidation

    語(yǔ)法:onValidation(ispass, runat, validationResult)

    對(duì)Model驗(yàn)證結(jié)果的監(jiān)聽方法

    • ispass

      驗(yàn)證是否通過(guò)的標(biāo)志

      • true

        驗(yàn)證通過(guò)

      • false

        驗(yàn)證不通過(guò)

    • runat

      返回進(jìn)行驗(yàn)證的端(客戶端或者服務(wù)器端)

      • client

        表示在客戶端進(jìn)行驗(yàn)證

      • server

        表示在服務(wù)器端進(jìn)行驗(yàn)證

    • validationResult

      驗(yàn)證返回信息

      1. newStudent.onValidation = function(ispass, runat, validationResult){ 
      2.  
      3.     if(ispass){console.log("Validation success !");} 
      4.  
      5.     console.log((runat=='client'?'Client':'Server')+(ispass==true?'Validation Success!':'Validation failed!')); 
      6.  
      7.     for(var i = validationResult.length-1; i>=0; i--){ 
      8.         console.log(runat=='client'?'Client':'Server')+'result is:'+validationResult[i].msg); 
      9.     } 
      10. }; 

      詳細(xì)代碼和說(shuō)明請(qǐng)參考《Examples》文檔。

#p#

Collection

Collection是Model的集合,我們之前曾使用過(guò)的subscribe()返回的結(jié)果集即是Collection。

  1. session.studentCollection = env.subscribe("pub-allStudents",function(myCollection){ 
  2.  
  3. }); 
  • create

    語(yǔ)法:create(dataMap)

    創(chuàng)建一個(gè)Collection

    1. sumeru.collection.create({'studnetName' : 'Smith'
    2.                                   'age' : 19, 
    3.                                'gender' : 'male' 
    4.                         }); 
  • size

    語(yǔ)法:size()

    獲取collection中包含Model的數(shù)量。

    session.studentCollection.size();
  • add

    語(yǔ)法:add(row)

    在collection中添加一行數(shù)據(jù)(每行數(shù)據(jù)實(shí)際是一個(gè)Model)。

    session.studentCollection.add(newStudent);
  • update

    語(yǔ)法:update(updateMap,where)

    更新collection中滿足條件的數(shù)據(jù)。

    session.studentCollection.update({'name':'Jack'},{'name':'John'});
  • remove

    語(yǔ)法:remove(where)

    將數(shù)據(jù)從collection中去除,但并不實(shí)際刪除。

    session.studentCollection.remove({'name':'John'});

    當(dāng)沒(méi)有參數(shù)時(shí),去除collection中所有數(shù)據(jù)。

  • destroy

    語(yǔ)法:destroy(where)

    將數(shù)據(jù)從collection中實(shí)際刪除。

    session.studentCollection.destroy({'name':'John'});

    當(dāng)沒(méi)有參數(shù)時(shí),刪除collection中所有數(shù)據(jù)。

  • setData

    語(yǔ)法:setData(dataMap)

    使用dataMap對(duì)Model賦值

  • find

    語(yǔ)法:find(where)

    查詢Collection中符合條件的所有數(shù)據(jù)。

    session.studentCollection.find({'name':'John'});

    當(dāng)沒(méi)有參數(shù)時(shí),返回所有的數(shù)據(jù)。

  • addSorters

    語(yǔ)法:addSorters()

    collection中添加排序方法

    session.studentCollection.addSorters('time','DESC')

    collection按照"time"降序排序。

  • clearSorters

    語(yǔ)法:clearSorters()

    清空collection中排序方法

    session.studentCollection.clearSorters();
  • applyStorters

    語(yǔ)法:applyStorters()

    手動(dòng)執(zhí)行所有的排序方法

    session.studentCollection.applyStorters();
  • get

    語(yǔ)法:get()

    根據(jù)下標(biāo)取出對(duì)應(yīng)的數(shù)據(jù)

    session.studentCollection.get(2);   
  • toJSON

    語(yǔ)法:toJSON()

    返回一個(gè)JSON對(duì)象

    session.studentCollection.toJSON();
  • getData

    語(yǔ)法:getData()

    獲取包含所有數(shù)據(jù)的數(shù)組

    session.studentCollection.getData();
  • save

    語(yǔ)法:save()

    將collection的修改保存到Server。

    session.studentCollection.save();
  • pluck

    語(yǔ)法:pluck(key)

    返回Collection某一字段所有數(shù)據(jù)的數(shù)組

    session.studentCollection.pluck('age');
  • hold

    語(yǔ)法:hold()

    暫停collection實(shí)時(shí)更新

    session.studentCollection.hold();   
  • releaseHold

    語(yǔ)法:releaseHold()

    恢復(fù)對(duì)collection的實(shí)時(shí)更新

    session.studentCollection.releaseHold();
  • where

    語(yǔ)法:where()

    在collection中指定查詢條件,需要與find、update、remove、destroy連用。

    1. session.studentCollection.where({'gender':'male'}); 
    2.  
    3. session.studentCollection.find(); 

    返回collection中‘gender’值為‘male’數(shù)據(jù)的數(shù)組。

  • orWhere

    語(yǔ)法:orWhere()

    在collection中添加一個(gè)“or”條件,需要與find、update、remove、destroy連用。

    1. session.studentCollection.orWhere({'gender':'male'}); 
    2.  
    3. session.studentCollection.find(); 
  • clearWheres

    語(yǔ)法:clearWheres()

    清空collection中所有查詢條件

    session.studentCollection.clearWheres()
責(zé)任編輯:張葉青
相關(guān)推薦

2013-10-31 14:55:22

2013-10-31 13:47:23

CloudaAPI

2013-10-31 14:30:44

CloudaAPI

2013-10-31 15:12:57

CloudaAPI

2010-05-20 19:12:37

2010-05-19 10:40:46

Subversion

2009-10-26 11:11:33

linux Emacs

2010-08-31 08:59:06

marginHTML

2009-12-02 18:03:00

PHP cURL

2011-08-09 13:22:31

iPhoneSqlite數(shù)據(jù)庫(kù)

2010-05-21 12:37:49

SVN使用教程

2010-05-26 14:01:47

SVN安裝使用手冊(cè)

2010-05-19 10:57:34

Subversion配

2010-05-26 12:59:48

SVN簡(jiǎn)易使用手冊(cè)

2010-05-26 13:17:55

SVN簡(jiǎn)易使用手冊(cè)

2010-06-07 12:38:37

Cacti使用手冊(cè)

2010-05-27 13:35:43

SVN簡(jiǎn)易使用手冊(cè)

2010-05-26 13:51:40

SVN安裝使用手冊(cè)

2010-06-09 17:01:26

Cacti使用手冊(cè)

2011-09-05 16:57:40

MTK開發(fā)工具
點(diǎn)贊
收藏

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