i created mywebsite having 2 types of users admin,user . So, i created 3pages mainpag.html, admin.html,user.html . and separate models,views,collections,routers.js files for each of them . So,after logging in, as i am sending to separate html pages with different models, i cant automatically get user model . so i did like this :
First, i made ajax call to server,asking for the _id (username in session,so i can get id)
from the id, i fetched the model ,by model.fetch(), then i got my usermodel with all attributes.
then in the success callback of fetch , i did model.save({weight : "somevalue"}) . According to me, it should update right , as the model is already available,that attribute weight also available with some old value, but it is sending POST request , also when i tried model.isNew() , it returned true . Where am i wrong ? how can i update my model ? I will post more details if required .
More details :
if i remove that save method, then i am getting correct attributes in the model.
if i dont remove that save method, that success and error callbacks are also appearing as attributes in the model.
Code :
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 : "+@user_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 "
})
the above code is written in coffeescript, so even if u dont know coffeescript,dont worry, u can understand easily, and those # means, it is a comment. here we follow indentation instead of braces.
one more doubt, a model's url must be changed dynamically according to the requirement , right ? what is the best way to achieve that ? i am doing like this :
i am populating "array" containing the required fields that should be present in the url . In model,s init func, i am using @arr=arr,thenin url's function , i check like this .
url : ->
if @arr['id']
"/user/#{@id}"
Is my approach right, or any better approach is there for dynamicaly setting url's . Or can i directly set the url's like this :
@user_model.setUrl "/someurl/someid" //this setUrl method is available in model's definition
@user_model.fetch() or save() or watever that needs url
thanks