Skip to content

canjs的数据绑定

jnotnull edited this page Nov 7, 2014 · 28 revisions

在上一篇关于canjs的文章中提到了组件开发,其中对于tag: "grid"的组件中的定义方法有各种令人生僻的用法:

can.Component.extend({
  tag: "grid",
  scope: {
	items: [],
	waiting: true
  },
  template: "<table><tbody><content></content></tbody></table>",
  events: {
	init: function () {debugger;
	  this.A();
	},
	"{scope} deferreddata": "A",
	A: function () {
	  var deferred = this.scope.attr('deferreddata'),
		scope = this.scope;
	  if (can.isDeferred(deferred)) {
		this.scope.attr("waiting", true);
		this.element.find('tbody').css('opacity', 0.5);
		deferred.then(function (items) {
		  scope.attr('items').replace(items);
		});
	  } else {
		scope.attr('items').attr(deferred, true);
	  }
	},
	"{items} change": function () {
	  this.scope.attr("waiting", false);
	  this.element.find('tbody').css('opacity', 1);
	}
  }
});
Clone this wiki locally