Backbone 1.0.0 版 API _ Backbone.Events 解析

本文详细介绍了Backbone.js中的事件系统,包括如何绑定、触发和解除事件绑定,以及一些高级用法如一次性绑定和跨对象事件监听。还列举了Backbone内置事件,并解释了如何阻止事件触发。

Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments. For example

var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
  alert("Triggered " + msg);
});
object.trigger("alert", "an event");

Backbone.Events是一个能够被任何对象继承的模块,如果一个对象继承Events后,将拥有Events的一些能力,即能绑定和触发自定义事件。使用Events不需要声明,只需要传入相应的参数即可。
For example, to make a handy event dispatcher that can coordinate events among different areas of your application:  var dispatcher = _.clone(Backbone.Events)
可以很简单的创建一个事件适配器,能够整合你的程序中不同地方用到的事件。

onobject.on(event, callback, [context])Alias: bind 
Bind a callback function to an object. The callback will be invoked whenever the eventis fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: "poll:start", or "change:selection". The event string may also be a space-delimited list of several events...

 

on是Backbone.Events的一个方法,通过extend继承后的对象就可以调用on方法,bind是on的别名:

Js代码 
  1. book.on === book.bind;  //true  

 on支持多个自定义事件同时绑定,用空格分割。

 

book.on("change:title change:author", ...);

To supply a context value for this when the callback is invoked, pass the optional third argument: model.on('change', this.render, this)

 

绑定的事件被激活后,可以通过第三个参数,传递上下文。

 

Callbacks bound to the special "all" event will be triggered when any event occurs, and are passed the name of the event as the first argument. For example, to proxy all events from one object to another:

 

有一个特殊的事件,即 all,任何事件的触发都会触发all绑定的事件,同时会将事件名作为第一个参数传递给all绑定的事件。

 

proxy.on("all", function(eventName) {
  object.trigger(eventName);
});

All Backbone event methods also support an event map syntax, as an alternative to positional arguments:

Backbone.Events的所有方法都支持事件map映射,如下更容易使用:

 

book.on({
  "change:title": titleView.update,
  "change:author": authorPane.update,
  "destroy": bookView.remove
});

offobject.off([event], [callback], [context])Alias: unbind 
Remove a previously-bound callback function from an object. If no context is specified, all of the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, callbacks for all events will be removed.

 

off移除绑定事件,别名unbind,最多有三个参数,都是可选项。如果没有参数的话,就是移除所有事件的所有callback,如果有一个参数就是event的所有callback;如果有两个参数就是移除event的callback;如果有三个参数,就是移除context身上绑定的event对应的callback方法。

event和callback可能为null,表示不指定具体某一个event或callback,而是移除所有event或者callback.

 

// Removes just the `onChange` callback.
object.off("change", onChange);

// Removes all "change" callbacks.
object.off("change");

// Removes the `onChange` callback for all events.
object.off(null, onChange);

// Removes all callbacks for `context` for all events.
object.off(null, null, context);

// Removes all callbacks on `object`.
object.off();

Note that calling model.off(), for example, will indeed remove all events on the model — including events that Backbone uses for internal bookkeeping.

  trigger object.trigger(event, [*args]) 

Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.

 

触发事件,支持map映射

Js代码 
  1. var object = {};  
  2. _.extend(object, Backbone.Events);  
  3. object.on("all"function (eventname) {  
  4.         alert(eventname);  
  5.     });  
  6. object.on({  
  7.         "test" : function (msg) {  
  8.             alert(msg);  
  9.         },  
  10.         "hello" : function (msg) {  
  11.             alert(msg);  
  12.         }  
  13.     });  
  14. object.trigger({  
  15.         "test" : "hello",  
  16.         "hello" : "hello world"  
  17.     });  

 

onceobject.once(event, callback, [context]) 
Just like on, but causes the bound callback to only fire once before being removed. Handy for saying "the next time that X happens, do this".

 

once就是on事件的一个简单的封装,即自定义事件触发一次后就移除callback。

 

listenToobject.listenTo(other, event, callback) 
Tell an object to listen to a particular event on an other object. The advantage of using this form, instead of other.on(event, callback), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on.

 

让一个对象监听另外一个对象发生的特殊事件,在表单应用中有优势。

 

view.listenTo(model, 'change', view.render);

stopListeningobject.stopListening([other], [event], [callback]) 
Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove all of its registered callbacks ... or be more precise by telling it to remove just the events it's listening to on a specific object, or a specific event, or just a specific callback.

相对于listenTo,停止监听。

 

view.stopListening();

view.stopListening(model);

listenToOnceobject.listenToOnce(other, event, callback) 
Just like listenTo, but causes the bound callback to only fire once before being removed.

只监听一次,一次后就stopListening

 

 

Catalog of Events 
Here's the complete list of built-in Backbone events, with arguments. You're also free to trigger your own events on Models, Collections and Views as you see fit. TheBackbone object itself mixes in Events, and can be used to emit any global events that your application needs.

下面的是Backbone.Events内置的一些儿事件,当然你也可以触发你绑定在Model上的自定义事件。

  • "add" (model, collection, options) — when a model is added to a collection.
  • "remove" (model, collection, options) — when a model is removed from a collection.
  • "reset" (collection, options) — when the collection's entire contents have been replaced.
  • "sort" (collection, options) — when the collection has been re-sorted.
  • "change" (model, options) — when a model's attributes have changed.
  • "change:[attribute]" (model, value, options) — when a specific attribute has been updated.
  • "destroy" (model, collection, options) — when a model is destroyed.
  • "request" (model, xhr, options) — when a model (or collection) has started a request to the server.
  • "sync" (model, resp, options) — when a model (or collection) has been successfully synced with the server.
  • "error" (model, xhr, options) — when a model's save call fails on the server.
  • "invalid" (model, error, options) — when a model's validation fails on the client.
  • "route:[name]" (params) — Fired by the router when a specific route is matched.
  • "route" (router, route, params) — Fired by history (or router) when any route has been matched.
  • "all" — this special event fires for any triggered event, passing the event name as the first argument.

Generally speaking, when calling a function that emits an event (model.set(),collection.add, and so on...), if you'd like to prevent the event from being triggered, you may pass {silent: true} as an option. Note that this is rarely, perhaps even never, a good idea. Passing through a specific flag in the options for your event callback to look at, and choose to ignore, will usually work out better.

整体来说,当触发一个事件的回调函数是,你也可以通过传递参数silent: true,来阻止回调函数的执行。


内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
在科技快速演进的时代背景下,移动终端性能持续提升,用户对移动应用的功能需求日益增长。增强现实、虚拟现实、机器人导航、自动驾驶辅助、手势识别、物体检测与距离测量等前沿技术正成为研究与应用的热点。作为支撑这些技术的核心,双目视觉系统通过模仿人类双眼的成像机制,同步获取两路图像数据,并借助图像处理与立体匹配算法提取场景深度信息,进而生成点云并实现三维重建。这一技术体系对提高移动终端的智能化程度及优化人机交互体验具有关键作用。 双目视觉系统需对同步采集的两路视频流进行严格的时间同步与空间校正,确保图像在时空维度上精确对齐,这是后续深度计算与立体匹配的基础。立体匹配旨在建立两幅图像中对应特征点的关联,通常依赖复杂且高效的计算算法以满足实时处理的要求。点云生成则是将匹配后的特征点转换为三维空间坐标集合,以表征物体的立体结构;其质量直接取决于图像处理效率与匹配算法的精度。三维重建基于点云数据,运用计算机图形学方法构建物体或场景的三维模型,该技术在增强现实与虚拟现实等领域尤为重要,能够为用户创造高度沉浸的交互环境。 双目视觉技术已广泛应用于多个领域:在增强现实与虚拟现实中,它可提升场景的真实感与沉浸感;在机器人导航与自动驾驶辅助系统中,能实时感知环境并完成距离测量,为路径规划与决策提供依据;在手势识别与物体检测方面,可精准捕捉用户动作与物体位置,推动人机交互设计与智能识别系统的发展。此外,结合深度计算与点云技术,双目系统在精确距离测量方面展现出显著潜力,能为多样化的应用场景提供可靠数据支持。 综上所述,双目视觉技术在图像处理、深度计算、立体匹配、点云生成及三维重建等环节均扮演着不可或缺的角色。其应用跨越多个科技前沿领域,不仅推动了移动设备智能化的发展,也为丰富交互体验提供了坚实的技术基础。随着相关算法的持续优化与硬件性能的不断提升,未来双目视觉技术有望在各类智能系统中实现更广泛、更深层次的应用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
Traceback (most recent call last): [rank0]: File "/data3/workspace/chenzh/Face-SVD/train.py", line 1668, in <module> [rank0]: main(**OmegaConf.load(args.config)) [rank0]: File "/data3/workspace/chenzh/Face-SVD/train.py", line 1409, in main [rank0]: discr_fake_pred = attr_discriminator(vt_hat_latents) [rank0]: File "/data1/miniconda3/envs/face_svd/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl [rank0]: return self._call_impl(*args, **kwargs) [rank0]: File "/data1/miniconda3/envs/face_svd/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl [rank0]: return forward_call(*args, **kwargs) [rank0]: File "/data1/miniconda3/envs/face_svd/lib/python3.10/site-packages/torch/nn/parallel/distributed.py", line 1632, in forward [rank0]: inputs, kwargs = self._pre_forward(*inputs, **kwargs) [rank0]: File "/data1/miniconda3/envs/face_svd/lib/python3.10/site-packages/torch/nn/parallel/distributed.py", line 1523, in _pre_forward [rank0]: if torch.is_grad_enabled() and self.reducer._rebuild_buckets(): [rank0]: RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by passing the keyword argument `find_unused_parameters=True` to `torch.nn.parallel.DistributedDataParallel`, and by [rank0]: making sure all `forward` function outputs participate in calculating loss. [rank0]: If you already have done the above, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's `forward` function. Please include the loss function and the structure of the return value of `forward` of your module when reporting this issue (e.g. list, dict, iterable). [rank0]: Parameters which did not receive grad for rank 0: backbone.encoder_block3.0.norm.bias, backbone.encoder_block3.0.norm.weight, backbone.encoder_block3.0.conv.weight, backbone.encoder_block2.2.attn.to_out.0.bias, backbone.encoder_block2.2.attn.to_out.0.weight, backbone.encoder_block2.2.attn.to_v.weight, backbone.encoder_block2.2.attn.to_k.weight, backbone.encoder_block2.2.attn.to_q.weight, backbone.encoder_block2.2.norm.bias, backbone.encoder_block2.2.norm.weight, backbone.encoder_block2.2.resnet.conv2.bias, backbone.encoder_block2.2.resnet.conv2.weight, backbone.encoder_block2.2.resnet.norm2.bias, backbone.encoder_block2.2.resnet.norm2.weight, backbone.encoder_block2.2.resnet.conv1.bias, backbone.encoder_block2.2.resnet.conv1.weight, backbone.encoder_block2.2.resnet.norm1.bias, backbone.encoder_block2.2.resnet.norm1.weight, backbone.encoder_block2.1.attn.to_out.0.bias, backbone.encoder_block2.1.attn.to_out.0.weight, backbone.encoder_block2.1.attn.to_v.weight, backbone.encoder_block2.1.attn.to_k.weight, backbone.encoder_block2.1.attn.to_q.weight, backbone.encoder_block2.1.norm.bias, backbone.encoder_block2.1.norm.weight, backbone.encoder_block2.1.resnet.conv2.bias, backbone.encoder_block2.1.resnet.conv2.weight, backbone.encoder_block2.1.resnet.norm2.bias, backbone.encoder_block2.1.resnet.norm2.weight, backbone.encoder_block2.1.resnet.conv1.bias, backbone.encoder_block2.1.resnet.conv1.weight, backbone.encoder_block2.1.resnet.norm1.bias, backbone.encoder_block2.1.resnet.norm1.weight, backbone.encoder_block2.0.norm.bias, backbone.encoder_block2.0.norm.weight, backbone.encoder_block2.0.conv.weight, backbone.encoder_block1.1.attn.to_out.0.bias, backbone.encoder_block1.1.attn.to_out.0.weight, backbone.encoder_block1.1.attn.to_v.weight, backbone.encoder_block1.1.attn.to_k.weight, backbone.encoder_block1.1.attn.to_q.weight, backbone.encoder_block1.1.norm.bias, backbone.encoder_block1.1.norm.weight, backbone.encoder_block1.1.resnet.conv2.bias, backbone.encoder_block1.1.resnet.conv2.weight, backbone.encoder_block1.1.resnet.norm2.bias, backbone.encoder_block1.1.resnet.norm2.weight, backbone.encoder_block1.1.resnet.conv1.bias, backbone.encoder_block1.1.resnet.conv1.weight, backbone.encoder_block1.1.resnet.norm1.bias, backbone.encoder_block1.1.resnet.norm1.weight, backbone.encoder_block1.0.attn.to_out.0.bias, backbone.encoder_block1.0.attn.to_out.0.weight, backbone.encoder_block1.0.attn.to_v.weight, backbone.encoder_block1.0.attn.to_k.weight, backbone.encoder_block1.0.attn.to_q.weight, backbone.encoder_block1.0.norm.bias, backbone.encoder_block1.0.norm.weight, backbone.encoder_block1.0.resnet.conv2.bias, backbone.encoder_block1.0.resnet.conv2.weight, backbone.encoder_block1.0.resnet.norm2.bias, backbone.encoder_block1.0.resnet.norm2.weight, backbone.encoder_block1.0.resnet.conv1.bias, backbone.encoder_block1.0.resnet.conv1.weight, backbone.encoder_block1.0.resnet.norm1.bias, backbone.encoder_block1.0.resnet.norm1.weight, backbone.conv1x1.bias, backbone.conv1x1.weight [rank0]: Parameter indices which did not receive grad for rank 0: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
05-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值