jQuery.Model.prototype.attr  function     

Gets or sets an attribute on the model using setters and getters if available.

$.Model("Recipe")
var recipe = new Recipe();
recipe.attr("foo","bar")
recipe.foo //-> "bar"
recipe.attr("foo") //-> "bar"

Setters

If you add a setAttributeName method on your model, it will be used to set the value. The set method is called with the value and is expected to return the converted value.

$.Model("Recipe",{
  setCreatedAt : function(raw){
    return Date.parse(raw)
  }
})
var recipe = new Recipe();
recipe.attr("createdAt","Dec 25, 1995")
recipe.createAt //-> Date

Asynchronous Setters

Sometimes, you want to perform an ajax request when you set a property. You can do this with setters too.

To do this, your setter should return undefined and call success with the converted value. For example:

$.Model("Recipe",{
  setTitle : function(title, success, error){
    $.post(
      "recipe/update/"+this.id+"/title",
      title,
      function(){
        success(title);
      },
      "json")
  }
})

recipe.attr("title","fish")

Events

When you use attr, it can also trigger events. This is covered in jQuery.Model.bind.

model.attr(attribute, value, success, error) -> undefined
{String}

the attribute you want to set or get

{optional:String|Number|Boolean}

value the value you want to set.

{optional:Function}

an optional success callback.
This gets called if the attribute was successful.

{optional:Function}

an optional success callback.
The error function is called with validation errors.

{undefined}
© Jupiter IT - JavaScriptMVC Training and Support