Clouda API使用手册之Router Model Collection

移动开发 Android
Clouda是简单,可依赖的实时Javascript框架。对一个想开发移动webapp的开发者来说,可以使用clouda开发框架,实现一个功能和体验与native app齐平的轻应用。

Router

用于建立URL(其路径部分)和Controller的对应关系,一个Controller可以对应多个URL,但是一个URL只能对应一个Controller。

  • add

    1. 语法:add({pattren:'', action:''}) 
    2.  
    3. 在router中添加一组pattren与Controller的对于关系 
    4.  
    5. sumeru.router.add( 
    6.     { 
    7.         pattern: '/studentList'
    8.         action: 'App.studentList' 
    9.     } 
    10. ); 

    • pattern

      URL(其路径部分的值)

    • action

      对应Controller的名称

    如果你想关闭Server渲染,可使用下面方法:

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

      Server渲染开关,false:关闭,默认为开启

  • setDefault

    语法:setDefault(controllerName)

    设置默认启动Controller

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

    语法:sumeru.router.externalProcessor.add(processor);

    添加外部处理器

    添加一个backbone的外部处理器  sumeru.router.externalProcessor.add(Backbone.Router.extend());

#p#

Model

Model用来定义App的数据模型。

  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

    字段的数据类型,包括一下数据类型:

类型 意义
int 整形
datetime 日期
string 字符串数
object 对象
array 数组
model 数据模型
collection 数据集合
  • relation

    使用relation时type属性值必须为“model”。

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

      引用一个Model

    • many

      引入一个Collection

  • defaultValue

    字段的默认值

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

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

    字段的验证,validation包括以下方法:

方法 意义
length[min,max] 字段值的长度在min-max的范围。
minlength(min) 字段值不小于min
maxlength(min) 字段值不大于min
required 字段值不能为空
unique 字段值必须唯一
telephone 字段值必须为电话号码格式
mobilephone 字段值必须为手机号码格式,长度为11位且必须为数字
email 字段值必须为email格式
onlyletter 字段值必须是字母
nospecialchars 字段值不能包含特殊字符
date 字段值必须是日期格式
url 字段值必须是URL
chinese 字段值必须是中文

注:多个验证条件之间使用" | "连接

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

    除了上面的验证方法外,还可以自定义验证方法。

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

      验证方法的名称,如"chinese"、"url"

    • runat

      定义在哪个端上(client/server)进行验证

      • client

        在客户端上进行验证

      • server

        在服务器端进行验证

      • both

        两段都需要验证

    • 验证方法:该API中框架提供三种自定义验证方法(三种方法(regxp/func/asyncFunc)每次只能使用一种

      • regxp

        使用自定义正则表达式对字段进行验证

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

        使用自定义函数对字段进行验证

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

        该验证函数在服务器端运行,先获取指定modelObj的数据,然后根据asyncFunc中的方法进行验证,在callback中给出验证的结果。

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

      验证失败后返回的信息

  • create

    语法:create(modelName)

    创建一个model

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

    newStudent.studentName = 'John';        
  • set

    语法:set(key,value)

    设置Model中相应字段的值

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

    语法:setData(dataMap)

    使用dataMap对Model赋值

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

    var name = newStudent.studentName;
  • get

    语法:get(key)

    获取某一字段的值

    newStudent.get('studentName');
  • getId

    语法:getId()

    获取model的唯一Id

    newStudent.getId();
  • getData

    语法:getData()

    返回一个JSON数据对象

    newStudent.getData();
  • destroy

    语法:destroy()

    删除model

    newStudent.destroy();
  • onValidation

    语法:onValidation(ispass, runat, validationResult)

    对Model验证结果的监听方法

    • ispass

      验证是否通过的标志

      • true

        验证通过

      • false

        验证不通过

    • runat

      返回进行验证的端(客户端或者服务器端)

      • client

        表示在客户端进行验证

      • server

        表示在服务器端进行验证

    • validationResult

      验证返回信息

      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. }; 

      详细代码和说明请参考《Examples》文档。

#p#

Collection

Collection是Model的集合,我们之前曾使用过的subscribe()返回的结果集即是Collection。

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

    语法:create(dataMap)

    创建一个Collection

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

    语法:size()

    获取collection中包含Model的数量。

    session.studentCollection.size();
  • add

    语法:add(row)

    在collection中添加一行数据(每行数据实际是一个Model)。

    session.studentCollection.add(newStudent);
  • update

    语法:update(updateMap,where)

    更新collection中满足条件的数据。

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

    语法:remove(where)

    将数据从collection中去除,但并不实际删除。

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

    当没有参数时,去除collection中所有数据。

  • destroy

    语法:destroy(where)

    将数据从collection中实际删除。

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

    当没有参数时,删除collection中所有数据。

  • setData

    语法:setData(dataMap)

    使用dataMap对Model赋值

  • find

    语法:find(where)

    查询Collection中符合条件的所有数据。

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

    当没有参数时,返回所有的数据。

  • addSorters

    语法:addSorters()

    collection中添加排序方法

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

    collection按照"time"降序排序。

  • clearSorters

    语法:clearSorters()

    清空collection中排序方法

    session.studentCollection.clearSorters();
  • applyStorters

    语法:applyStorters()

    手动执行所有的排序方法

    session.studentCollection.applyStorters();
  • get

    语法:get()

    根据下标取出对应的数据

    session.studentCollection.get(2);   
  • toJSON

    语法:toJSON()

    返回一个JSON对象

    session.studentCollection.toJSON();
  • getData

    语法:getData()

    获取包含所有数据的数组

    session.studentCollection.getData();
  • save

    语法:save()

    将collection的修改保存到Server。

    session.studentCollection.save();
  • pluck

    语法:pluck(key)

    返回Collection某一字段所有数据的数组

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

    语法:hold()

    暂停collection实时更新

    session.studentCollection.hold();   
  • releaseHold

    语法:releaseHold()

    恢复对collection的实时更新

    session.studentCollection.releaseHold();
  • where

    语法:where()

    在collection中指定查询条件,需要与find、update、remove、destroy连用。

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

    返回collection中‘gender’值为‘male’数据的数组。

  • orWhere

    语法:orWhere()

    在collection中添加一个“or”条件,需要与find、update、remove、destroy连用。

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

    语法:clearWheres()

    清空collection中所有查询条件

    session.studentCollection.clearWheres()
责任编辑:张叶青
相关推荐

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

2009-10-26 11:11:33

linux Emacs

2010-05-19 10:40:46

Subversion

2011-08-09 13:22:31

iPhoneSqlite数据库

2009-12-02 18:03:00

PHP cURL

2010-08-31 08:59:06

marginHTML

2010-05-26 14:01:47

SVN安装使用手册

2010-05-21 12:37:49

SVN使用教程

2010-05-19 10:57:34

Subversion配

2010-05-26 13:17:55

SVN简易使用手册

2010-05-26 12:59:48

SVN简易使用手册

2010-06-07 12:38:37

Cacti使用手册

2010-05-27 13:35:43

SVN简易使用手册

2010-05-26 13:51:40

SVN安装使用手册

2010-08-25 10:53:04

CSSmargin-bott

2010-06-09 17:01:26

Cacti使用手册
点赞
收藏

51CTO技术栈公众号