lundi 4 mai 2015

Scope of Ember injected dependencies in Controllers

I am wondering how injected objects are scoped in controllers.

Say I create a service to return an array of days in months, and i register it and inject it in a controller called BookingsController, like so

Todos.Bookingmonth = Ember.Object.extend({
      currMonth: 1,
      currYear : 2014,
      names : [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ],
      //
      setCurrMonthYear: function(currMonth, currYear){
        //
        this.currMonth = parseInt(currMonth, 10);
        this.currYear = parseInt(currYear, 10);
      },
      //
      getDaysArray : function () {
          //
          var date = new Date(this.currYear, this.currMonth - 1, 1),
              days = [];

          while (date.getMonth() === this.currMonth - 1) {
            //
            days.push({num : date.getDate(), name : this.names[date.getDay()]});
            date.setDate(date.getDate() + 1);
          }
          //
          return days;
      }
    });

    Todos.register('bookingmonth:main', Todos.Bookingmonth);

    Todos.inject('controller:bookings', 'bookingmonth', 'bookingmonth:main');



Todos.BookingsController = Ember.ArrayController.extend({
  title : "Bookings listing",
  monthDays : this.get('bookingmonth').getDaysArray(),// this.get is   undefined!!
  actions: {

  }
});

The reason I want to do this, is because I need to access n array of dates within my Bookings template, but this is unrelated to application logic, neither it belongs with the main model data (which is a Fixture of bookings, by the way). I just wanted to generate the array in my controller and assign it to a property (as per above code), then loop it in the template, like so

<ul id="month-bookings-slots">
  {{#each slot in monthDays}}
    <li>SLOT</li>
  {{/each}}
</ul>

I know I could probably move the while dates logic from the service into the controller itself, as a 'private' method of the controller, and would probably be then easier to assign its returned array of dates to a generic property of the controller, but wanted to try and do it via a service as it seems better for logic separation

Maybe I am getting a bit confused about the scope of injected dependencies, would be great to hear what I'm missing / any suggestion. Thanks




Aucun commentaire:

Enregistrer un commentaire