发布于 2015-08-14 15:15:16 | 127 次阅读 | 评论: 0 | 来源: 网络整理
它就像事件,在被删除之前绑定回调就触发一次。
object.once(event, callback function, [context])
<!DOCTYPE html>
<head>
<title>Event Once Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
//The created object 'myVal' is extended using Backbone.Events method
var myVal = _.extend({name:'YiiBai!!!'}, Backbone.Events);
//The once() method causes the bound callback to only fire once before being removed
myVal.once('hello', function () {
document.write("The value after firing once is: ");
document.write(this.name);//name will get displayed by referring the current object
});
//It triggers the 'hello' event on object 'myVal'
myVal.trigger('hello');
</script>
</body>
</html>
让我们进行以下步骤来看看上面的代码工作:
保存上述代码在文件once.html
在浏览器中打开这个HTML文件。