- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个地理位置模型,从 localStorage 获取数据,并检查那里是否有纬度属性。如果不存在,则会触发 'geolocation:city_state_blank'
事件。如果听到 'geolocation:city_state_blank'
,则会触发 @get_users_geolocation()
。
class App.GeoLocation extends Backbone.Model
initialize: ->
@on 'geolocation:city_state_blank', -> @get_users_geolocation()
@fetch().always =>
if @get('city_state').length is 0
@trigger 'geolocation:city_state_blank'
else
@trigger('geolocation:done')
get_users_geolocation: =>
# Code to get / save (in localStorage) a users geolocation and city / state
@trigger('geolocation:done')
完成get_users_geolocation()
后,会触发geolocation:done
事件。
我删除了获取用户地理位置/反向地理位置查找的详细信息,这些都是异步的。但所有这些工作的最终结果都归结为触发 geolocation:done
事件。
class App.GeolocationView extends Backbone.View
initialize: =>
@listenTo @model, 'geolocation:done', =>
alert('geolocation:done was heard in the view!!!!')
在这种情况下,本地理位置模型从 localStorage 获取并确定属性 latitude
未设置,从而调用 get_users_geolocation
时, View 警报地理定位:在 View 中听到完成!!!
但在 Geolocation 具有纬度属性(else)并立即触发 geolocation:done
的情况下, View 不会发出任何警报。 View 听不到它。
我已经控制台记录了这一切,并且可以说流路径正在工作。 if/else 正在工作,并且 View 正在正确实例化。从 localStorage 获取后在回调中记录会产生此..
@fetch().always =>
console.log @get('current_city_state')
console.log typeof @get('current_city_state')
// Norfolk, VA
// String
所以,那里有数据..
这是怎么回事??请帮忙!!
最佳答案
我猜您的 App.GeoLocation
的 latitude
属性是一个数字。这会让你的测试看起来像这样:
if some_number.length is 0
数字没有 length
属性,因此 @get('latitude').length
将是未定义的,您将剩下:
if undefined is 0
这总是错误的,因此 @trigger('geolocation:done')
总是被调用。
如果您对 latitude
属性的存在感兴趣,那么 CoffeeScript 的存在运算符 (?
) 将为您提供更好的服务:
if @get('latitude')?
# There is a latitude.
else
# No latitude.
要了解 ?
的行为,您可以看看它的作用:
class M extends Backbone.Model
m = new M(p: 0)
if m.get('p') == 0
console.log('p == 0')
else
console.log('p != 0')
if m.get('p')?
console.log('there is a p')
else
console.log('the p is a lie')
if m.get('no')?
console.log('yes no')
else
console.log('no no')
这将为您提供p == 0
,有一个p
,以及no no
。
关于javascript - Backbone Model 有条件地触发事件,View 听不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445438/
是否可以使用回调来监听 CSS 变量的变化?像这样的东西: documentElement.addListener('css-var-main-background-color', () => {
我有应该运行laravel队列的docker镜像。在图像中,我有php artisan queue:listen命令,但是当我启动容器时,什么也没有发生,并且容器在运行。 这是基本图片 FROM ph
我正在尝试下载视频以在 exoplayer 中离线播放,但我不知道如何收听 onDownloadComplete。在 exoplayer 文档中,他们说 DownloadService是一个环绕 an
如果我在 View Controller 上有 2 个通知有 2 个这样的观察者,这是否合适: [NSNotificationCenter defaultCenter] addObserver:sel
尝试创建服务器-客户端应用程序时,我在设置服务器端连接时遇到了一些麻烦。设置套接字并bind()ing 套接字后,我的listen() 调用失败并显示错误消息 listen: Invalid argu
我有一个从不同模块更新的 UITextField(我将我的 UITextField 传递给上面提到的模块以填充它)。我需要一种方法来识别 UITextField 的文本值何时在 UITextField
假设我有单独的 Django 和 Tornado 应用程序。这就是我想要实现的目标: Django 收到一些 POST 请求并通知 Tornado 应用程序,以便 Tornado 应用程序可以向其连接
在一个模块上,我需要监听 url 的变化,所以我写了一个简单的代码: componentDidMount(){ history.listen( location => {
我想断言,当单击禁用按钮时,不会触发其 onClick 事件。我如何用 enzyme 来做到这一点?请参阅下面的示例代码。谢谢! sampleButton.jsx: import React from
我遇到了这个问题,我有一个部分(“.aboutsection”)一开始是隐藏的,需要在点击网页上的某个点时显示出来,然后开始为 .slideDown() 设置动画。我让它工作了,但每次我滚动到那个特定
我有一个关于 JS 中的键盘事件的问题。 首先,请不要回答我使用 jQuery 方法,我知道其中的大部分(绑定(bind)/解除绑定(bind),开/关,一个......)但我使用内部框架必须在没有
shiny 中是否有一个暴露的(甚至可能是非暴露的)函数会告诉我什么时候 react 失效?示例草稿: ui.R selectInput('select', ...) 服务器.R myfunc <-
我正在使用 PostgreSQL 数据库并应用它的 LISTEN/NOTIFY 功能。所以我的监听器在我的 AS(应用程序服务器)上,我在我的数据库上配置了触发器,这样当在表上执行 CRUD 操作时,
设置:一个简约的 Linux(华硕路由器上的 OpenWRT),一个 USB 键盘(假设我知道设备名称,如/dev/hiddev0) 一个目标:编写一个 python(或 shell,在这种情况下我可
我使用 Fragment 让用户在我的 Android 应用程序中输入信息。如果输入了信息,我希望在用户按下“后退”键时向用户发送警告。如果他确认要放弃更改,则必须弹出 Fragment。如果他取消放
这是我的 HTML/CSS 当前的样子: 这是我想要它的样子: 我如何修改下面的 HTML/CSS 以使其显示我想要的样子? HTML: CSS: .upda
我想使用 Heroku、PostgreSQL 和 Node.js,并将其设置为每当我在我的 postgres 数据库中添加一条记录时,Node.js 都会将该行的内容打印到控制台。 我正尝试按照这些指
在今天晚上之前我卸载了 vscode 并再次安装后一切正常我在尝试运行 npm run app 时遇到了这个问题我不知道如何解决这个问题我试图运行 vs 代码和命令提示符或 bash 使用以管理员身份
我目前正面临一个我无法解决的错误,我已经苦苦挣扎了几个小时。 我正在使用以下版本: Node :8.11.3 express :4.16.3 Jest :22.2.2 Mongoose :5.2.3
我正在尝试为网站上的特定 url 部署使用 node.js 开发的服务器。 已经有一个服务器可以覆盖网站上的所有网址,我希望我的唯一答案是 the_website_url/my_specific_ur
我是一名优秀的程序员,十分优秀!