我創建了具有2種類型的用戶admin,user的mywebsite。所以,我創建了3頁mainpag.html,admin.html,user.html。併爲它們中的每一個單獨模型,視圖,集合,routers.js文件。所以,登錄後,因爲我發送到不同模型的HTML頁面,我不能自動獲取用戶模型。所以我不喜歡這樣的:主幹模式,設置屬性(修改),然後保存模型,它應該更新但發送POST請求
首先,我做了Ajax調用服務器,要求爲_id(用戶名在會議上,這樣我就可以得到ID)從ID
,我拿來的模型,通過模型。 fetch(),然後我得到了我的用戶模型的所有屬性。
然後在fetch的成功回調中,我做了model.save({weight:「somevalue」})。據我說,它應該更新正確,因爲模型已經可用,屬性權重也可用一些舊的價值,但它是發送POST請求,也是當我嘗試model.isNew()時,它返回true。我在哪裏錯了?我如何更新我的模型?如果需要,我會發布更多細節。
更多細節:
如果我刪除保存方法,然後我得到的模型正確的屬性。
如果我不刪除該保存方法,成功和錯誤回調也出現在模型中的屬性。
代碼:
addWeight : (e)->
arr=new Array()
arr['_id']=app._id
console.log "asdasd"
console.log arr
console.log arr['_id']
@user_model =new UserModel(arr)
@user_model.fetch({
success : (model,res,options) =>
console.log model
console.log res
arr=new Array()
arr['_id']=e.target.id
#arr['action']='weight' #means , update weight
#@user_model.setArr(arr)
#@user_model.set({weight : arr['_id']})
console.log "new : "[email protected]_model.isNew()
@user_model.save({weight : e.target.id})
#@user_model.save({
# success : (model,res,options) =>
# console.log "model updated: "+JSON.stringify(model)
# console.log "Res : "+JSON.stringify(res)
# error : (model,res,options) =>
# console.log "Error : "+JSON.stringify(res)
#})
error : (model,res,options) =>
console.log "Error "
})
上面的代碼寫成的CoffeeScript,所以即使ü不知道CoffeeScript的,不要擔心,美國可以很容易地理解,而那些#手段,這是一條評論。這裏我們遵循縮進而不是大括號。
還有一個疑問,模型的url必須根據需要動態改變,對吧?達到這個目標的最好方法是什麼?我這樣做:
我正在填充「數組」,其中包含應該存在於url中的必需字段。在模型中,s init func,我使用@ arr = arr,然後在url的函數中,我檢查像這樣。
url : ->
if @arr['id']
"/user/#{@id}"
我的方法是否正確,或者有更好的方法來設置url的動態設置。或者,我可以直接設置的URL是這樣的:
@user_model.setUrl "/someurl/someid" //this setUrl method is available in model's definition
@user_model.fetch() or save() or watever that needs url
感謝
我試過了也,它發送POST請求,那是我的問題,無論如何,我將發佈我的代碼在2分鐘..] – user1305989 2012-04-18 06:09:56
我剛纔在問題中添加了代碼 – user1305989 2012-04-18 06:18:02
剛剛有一個想法,模型被認爲是新的,如果它沒有「id」屬性,在這裏我沒有「id」屬性,我有「_id」屬性,,我的數據庫使用「_id」作爲默認值,所以我正在使用它。那麼這裏的問題呢? – user1305989 2012-04-18 06:21:19