【发布时间】:2015-03-14 19:02:05
【问题描述】:
我已经定义了我的模型和它的默认变量;然而,每当我尝试在模型本身中使用这些变量中的任何一个时,它们的值都会设置为“未定义”。
JAWeatherWatch.module('Cities', function(Cities, JAWeatherWatch, Backbone, Marionette){ /* 城市 * 此模型用于对开放天气 api 返回的数据进行建模 * 牙买加的每个城市 */ Cities.City = Backbone.Model.extend({ 默认值:{ // 存储我的 api 密钥以及添加到请求中所需的查询字符串 apiKey: '&APPID=84cb7efaf4ac2947aa6381637904ee5e', 国家:'牙买加', 教区名称:假 }, url:'http://api.openweathermap.org/data/2.5/weather?q=' + this.parishName + "," + this.country + this.apiKey, 初始化:函数(){ console.log(this.country); // => '未定义' // todo: 从 api 获取 json 数据 } }); }); var test = new JAWeatherWatch.Cities.City({parishName:'kingston'}); console.log(test.get('country')) // => 'jamaica'奇怪的是,在模型实例化后它们的行为符合预期(即返回正确的值)。我完全不知道为什么会这样
【问题讨论】:
-
是的,因为
this.country真的是未定义的使用this.get('country') -
我试过了,得到以下错误:'TypeError: this.get is not a function'
标签: javascript backbone.js marionette