Clouda API使用手冊(cè)之Router Model Collection
Router
用于建立URL(其路徑部分)和Controller的對(duì)應(yīng)關(guān)系,一個(gè)Controller可以對(duì)應(yīng)多個(gè)URL,但是一個(gè)URL只能對(duì)應(yīng)一個(gè)Controller。
-
add
- 語(yǔ)法:add({pattren:'', action:''})
- 在router中添加一組pattren與Controller的對(duì)于關(guān)系
- sumeru.router.add(
- {
- pattern: '/studentList',
- action: 'App.studentList'
- }
- );
-
pattern
URL(其路徑部分的值)
-
action
對(duì)應(yīng)Controller的名稱
如果你想關(guān)閉Server渲染,可使用下面方法:
- sumeru.router.add(
- {
- pattern: '/studentList',
- action: 'App.studentList'
- server_render:false
- }
- )
-
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ù)模型。
- Model.student = function(exports){
- exports.config = {
- fields: [
- {name : 'studentName', type: 'string'},
- {name : 'age', type: 'int'},
- {name : 'gender', type: 'string'}
- ]
- };
- };
屬性
-
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格式 | |
onlyletter | 字段值必須是字母 |
nospecialchars | 字段值不能包含特殊字符 |
date | 字段值必須是日期格式 |
url | 字段值必須是URL |
chinese | 字段值必須是中文 |
注:多個(gè)驗(yàn)證條件之間使用" | "連接
{name: 'name', type: 'string', validation:'length[1,20]|required'},
-
addRule
除了上面的驗(yàn)證方法外,還可以自定義驗(yàn)證方法。
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- 驗(yàn)證方法 ,
- "msg" : "",
- });
-
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)證
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "regxp" : "()",
- "msg" : "",
- });
-
func
使用自定義函數(shù)對(duì)字段進(jìn)行驗(yàn)證
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "func" : function(){},
- "msg" : "",
- });
-
asyncFunc
該驗(yàn)證函數(shù)在服務(wù)器端運(yùn)行,先獲取指定modelObj的數(shù)據(jù),然后根據(jù)asyncFunc中的方法進(jìn)行驗(yàn)證,在callback中給出驗(yàn)證的結(jié)果。
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "asyncFunc":function(callback,k,v,modelObj){}
- "msg" : "",
- });
-
-
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賦值
- newStudent.setData({'studnetName' : 'Smith',
- 'age' : 19,
- 'gender' : 'male'
- });
-
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)證返回信息
- newStudent.onValidation = function(ispass, runat, validationResult){
- if(ispass){console.log("Validation success !");}
- console.log((runat=='client'?'Client':'Server')+(ispass==true?'Validation Success!':'Validation failed!'));
- for(var i = validationResult.length-1; i>=0; i--){
- console.log(runat=='client'?'Client':'Server')+'result is:'+validationResult[i].msg);
- }
- };
詳細(xì)代碼和說(shuō)明請(qǐng)參考《Examples》文檔。
-
#p#
Collection
Collection是Model的集合,我們之前曾使用過(guò)的subscribe()返回的結(jié)果集即是Collection。
- session.studentCollection = env.subscribe("pub-allStudents",function(myCollection){
- });
-
create
語(yǔ)法:create(dataMap)
創(chuàng)建一個(gè)Collection
- sumeru.collection.create({'studnetName' : 'Smith',
- 'age' : 19,
- 'gender' : 'male'
- });
-
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連用。
- session.studentCollection.where({'gender':'male'});
- session.studentCollection.find();
返回collection中‘gender’值為‘male’數(shù)據(jù)的數(shù)組。
-
orWhere
語(yǔ)法:orWhere()
在collection中添加一個(gè)“or”條件,需要與find、update、remove、destroy連用。
- session.studentCollection.orWhere({'gender':'male'});
- session.studentCollection.find();
-
clearWheres
語(yǔ)法:clearWheres()
清空collection中所有查詢條件
session.studentCollection.clearWheres()