Emberjs Uncaught TypeError:Object#<object>没有方法'get'</object>

时间:2014-04-05 20:57:49

标签: javascript ember.js

我尝试使用模型创建一个动态路线,该路线会在网址中显示日期,但我收到了一个typedef错误,而我无法看到哪个获得了该信息。&#39;谈论它以及为什么它给我错误。 这是我的模型或我的路线给我错误。当我尝试创建动态路由时,如何避免这种情况发生?什么是一个很好的资源或书籍,可以告诉我Ember的目的和结局?

    var App = Ember.Application.create({
        LOG_TRANSITIONS: true
    });

    App.Router.map(function(year, month, date) {
        this.resource('calendar');
        this.resource('about'); 
        this.resource('utcDate', {path: ':utcDate'});
    });

    App.IndexRoute = Ember.Route.extend({
        redirect: function(){
            this.transitionTo('calendar');
        }
    });

    App.CalendarRoute = Ember.Route.extend({
        model: function(){
            return App.CalendarData.create();
        },
        model: function(params, transition){
            return {utcDate: params.utcDate}
        },
        serializer: function(model){
            return {utcDate: model.get('utcDate')};
        },
        serializer: function(model){
            return {month: model.get('Today')};
        }
    });

    // calendar controller
    App.CalendarController = Ember.Controller.extend({

//something here seems to be throwing an error that I can't troubleshoot       
      utcDate: function() {
        return this.get("model").get("utcDate");
      }.property("model"),

      days: function() {
        return this.get("model").get("days");
      }.property("model"),

      date: function() {
        return this.get("model").get("date");
      }.property("model"),

      month: function(){
        return this.get('model').get('month');
      }.property('model'),

      dow: function(){
        return this.get('model').get('dow');
      }.property('model'),

      year: function(){
        return this.get('model').get('year');
      }.property('model')
    });

    // calendar data
    App.CalendarData = Ember.Object.extend({
        today: new Date(),

        utcDate: function(){
          return this.get('today').getMonth()+1 + '/'+ this.get('today').getDate()+'/'+this.get('today').getFullYear();
        }.property('today'),

        date: function(){
          return this.get("today").getDate();
        }.property('today'),

        days: function(){
          var Mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
          var Ndays =  Mdays[this.get('today').getMonth()];
          var days = [];
          var cDays = 0;
          for (var i = 0; i < 5; i++) {
            days[i] = [];
              for (j = 0; j <=6; j++) {
                if (cDays <= Ndays){
                  days[i][j] = cDays++;
                } else{
                  days[i][j] = " "
                }
              }
          }
          return days;
        }.property('today'),

        month: function(){
          var MonthA = ["January", "February", "March", "April", "May", "June", "July",  "August", "September", "October", "November", "December"];
          var month = MonthA [this.get('today').getMonth()];
          return month;
        }.property('today'),

        dow: function(){
          var Weekday = ["Sunday", "Monday", "Tuesday", "Wednesday",  "Thursday", "Friday", "Saturday"];
          var dow= Weekday[this.get('today').getDay()];
          return dow;
        }.property('today'),

        year: function(){
          return this.get('today').getFullYear();
        }.property('today')
    });

http://emberjs.jsbin.com/yicuh/1/

1 个答案:

答案 0 :(得分:1)

您已多次定义model挂钩和serialize挂钩。

App.CalendarRoute = Ember.Route.extend({
    model: function(){
        return App.CalendarData.create();
    },
    model: function(params, transition){
        return {utcDate: params.utcDate}
    },
    serializer: function(model){
        return {utcDate: model.get('utcDate')};
    },
    serializer: function(model){
        return {month: model.get('Today')};
    }
});

我猜我选择了其中一个,但另外如果一个对象不是一个ember对象,你需要使用Ember.get(object, property)而不是object.get(property)

App.CalendarRoute = Ember.Route.extend({
    model: function(){
        return App.CalendarData.create();
    },
    serializer: function(model){
        return {utcDate: Em.get(model,'utcDate')};
    },
});

http://emberjs.jsbin.com/wemoc/1/edit