Clouda API使用手冊之Router Model Collection
Router
用于建立URL(其路徑部分)和Controller的對應關系,一個Controller可以對應多個URL,但是一個URL只能對應一個Controller。
-
add
- 語法:add({pattren:'', action:''})
- 在router中添加一組pattren與Controller的對于關系
- sumeru.router.add(
- {
- pattern: '/studentList',
- action: 'App.studentList'
- }
- );
-
pattern
URL(其路徑部分的值)
-
action
對應Controller的名稱
如果你想關閉Server渲染,可使用下面方法:
- sumeru.router.add(
- {
- pattern: '/studentList',
- action: 'App.studentList'
- server_render:false
- }
- )
-
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的數據模型。
- Model.student = function(exports){
- exports.config = {
- fields: [
- {name : 'studentName', type: 'string'},
- {name : 'age', type: 'int'},
- {name : 'gender', type: 'string'}
- ]
- };
- };
屬性
-
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格式 | |
onlyletter | 字段值必須是字母 |
nospecialchars | 字段值不能包含特殊字符 |
date | 字段值必須是日期格式 |
url | 字段值必須是URL |
chinese | 字段值必須是中文 |
注:多個驗證條件之間使用" | "連接
{name: 'name', type: 'string', validation:'length[1,20]|required'},
-
addRule
除了上面的驗證方法外,還可以自定義驗證方法。
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- 驗證方法 ,
- "msg" : "",
- });
-
ruleName
驗證方法的名稱,如"chinese"、"url"
-
runat
定義在哪個端上(client/server)進行驗證
-
client
在客戶端上進行驗證
-
server
在服務器端進行驗證
-
both
兩段都需要驗證
-
-
驗證方法:該API中框架提供三種自定義驗證方法(三種方法(regxp/func/asyncFunc)每次只能使用一種)
-
regxp
使用自定義正則表達式對字段進行驗證
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "regxp" : "()",
- "msg" : "",
- });
-
func
使用自定義函數對字段進行驗證
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "func" : function(){},
- "msg" : "",
- });
-
asyncFunc
該驗證函數在服務器端運行,先獲取指定modelObj的數據,然后根據asyncFunc中的方法進行驗證,在callback中給出驗證的結果。
- sumeru.validation.addRule(ruleName,{
- "runat" : "client",
- "asyncFunc":function(callback,k,v,modelObj){}
- "msg" : "",
- });
-
-
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賦值
- newStudent.setData({'studnetName' : 'Smith',
- 'age' : 19,
- 'gender' : 'male'
- });
-
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
驗證返回信息
- 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);
- }
- };
詳細代碼和說明請參考《Examples》文檔。
-
#p#
Collection
Collection是Model的集合,我們之前曾使用過的subscribe()返回的結果集即是Collection。
- session.studentCollection = env.subscribe("pub-allStudents",function(myCollection){
- });
-
create
語法:create(dataMap)
創建一個Collection
- sumeru.collection.create({'studnetName' : 'Smith',
- 'age' : 19,
- 'gender' : 'male'
- });
-
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連用。
- session.studentCollection.where({'gender':'male'});
- session.studentCollection.find();
返回collection中‘gender’值為‘male’數據的數組。
-
orWhere
語法:orWhere()
在collection中添加一個“or”條件,需要與find、update、remove、destroy連用。
- session.studentCollection.orWhere({'gender':'male'});
- session.studentCollection.find();
-
clearWheres
語法:clearWheres()
清空collection中所有查詢條件
session.studentCollection.clearWheres()