Search code examples
jquerydeferredjsrender

JSView passing context to deferred object


I tried to passing the data context to a deferred object to modify the data after execute a function:

function confirmBox(obj){
  var defer = $.Deferred();
  //cut the function
  //if true, it will returned the obj, by false it will be defer.reject()
  defer.resolve(obj);
  return defer.promise();
}

$.link.testTemplate("#main", app)
  .on("click", ".test-item", function(e){
    confirmBox($.view(this)).then(function (answer) {
      console.log(answer.getIndex())
    });
    //console.log($.view(this).getIndex()) => working fine
  })

It returned an undefined value.

Where are my mistake?


Solution

  • view.getIndex() will return undefined if the context (view) is not an 'item' view, or within an item view.

    http://www.jsviews.com/#getindex
    http://www.jsviews.com/#viewobject@index

    So if your 'test-item' element is not within a {^{for someArray}} index will be undefined.

    Your example works fine for me when 'test-item' is in an item view...

    Otherwise, can you post a complete example to jsfiddle...