5

I have the following setup for displaying my order by date: orders by day

I have the following template that shows each order for a given week: (I stripped some html for brevity)

Template:

{{#each ordersByDate in ordersByDateOfWeek}}
    <div class="orders-by-date">
        <div>
            <span>{{order-date-formatted ordersByDate.date}}</span>
        </div>

        {{#each order in ordersByDate.orders}}
            {{order.number}} {{! updates correctly}}
            {{order.market.name}} {{! a hasmany property called here, does not update}}
        {{/each}}
    </div>
{{/each}

The computed property:

ordersByDateOfWeek: function () {

    var result = []; // return value
    var object = null
    var date = null;


    // add every order to an array with a date an order objects
    this.forEach(function (order) {

        // get the date of the order 
        date = order.get('created');

        // create a new object that holds the orders for a given date
        object = result.findProperty('date', date);

        // create the object if it does not exist
        if (!object) {
            object = {
                date: date,
                orders: [],
                // some more
            };

            // add the object with the date to the result
            result.push(object);
        }

        // add the orders to the current object
        object.orders.push(order);

        // more calculations done here below
    });
}.property('model', 'sortProperties', 'sortAscending'),

What it does is, that it creates an array of objects like this:

[
   {
     "date":"2014-12-08",
     "orders":[// order objects here],
     // some more properties
   },
   {
     "date":"2014-11-08",
     "orders":[],
   },
   {
     "date":"2014-10-08",
     "orders":[],
   },
]

I am trying for hours now, and cannot get my head around it to get it to work. My intuition says that it has to do with promises? But in the "orders: []" array are real Ember objects, so it should have to work I think.

I hope that someone can point me in the right direction.

Many thanks guys!

Edit: to be 100% clear: my order model consists solely of orders. That custom object do I create myself. Thats why the binding of data gets broken I think.

DelphiLynx
  • 911
  • 1
  • 16
  • 41
  • Have you checked out `Ember.computed.sort`? Also there's a standard property `arrangedContent` that is used as the result of a sort if you're looking for that sort of thing. – aceofspades Dec 12 '14 at 01:11
  • Hi, I am a bit late, but thanks for your answer! However I do not see how this will work because `arrangedContent` is also used for `sortProperties` which I already use. See: http://emberjs.com/api/classes/Ember.ArrayController.html#property_arrangedContent Do I have to write my own `arrangedContent` with my own sorting build in? If you provide an working example in an answer I will accept it as such. – DelphiLynx Dec 17 '14 at 10:39
  • After days of fiddling with it I do a bounty on it. I can't get where I want, because arranged content does not have the possibility to have a date before the original items. – DelphiLynx Dec 19 '14 at 08:19
  • @DelphiLynx could you, please, share your fiddle so that we can work with a base ? – axelduch Dec 19 '14 at 09:11
  • A jsbin/jsfiddle would definitely be helpful... – Kalman Dec 19 '14 at 14:08
  • as said by others we need more code likely what does your model look like... – MrVinz Dec 23 '14 at 04:58
  • It is already fixed by the complete answer of @Lazybensch – DelphiLynx Jan 05 '15 at 09:42

2 Answers2

5

The problem is that your template only updates itself when ordersByDateOfWeek updates. Which happens whenever any of its watched properties changes ('model', 'sortProperties', 'sortAscending'). When you build order objects yourself, under the hood ember will add them as you want but they will not cause the ordersByDateOfWeek to update and hence your template will not recognise the change.

The easiest solution would be to work with actual properties, (i.e. your orders models) and add them with 'model.@each.orders' to ordersByDateOfWeeks watch list. If you need extra fields like date create them as computed properties on the model, like so:

date: Ember.computed.alias('created')

You should rarely need to create objects by hand.

Lazybensch
  • 506
  • 2
  • 5
  • Thanks for your complete answer. A bit late, however I was not on the project last time. Your answer did the trick, I have edited the watch list of the computed properties, and added `model.@each`. (orders is not a has-many in my case) – DelphiLynx Jan 05 '15 at 09:42
2

Computed properties are not magic ;-) They are great, but not magic.

I suspect that since you are listening on changes for 'model', it does not trigger the update when you change properties nested inside this object. Your property will only be re-evaluated when the object reference 'model' gets changed.

In this case you need at least 'model.@each.orders' if you actually assign a new Array to your objects. If you just change the array by adding more objects later on, you need a two step solution. If this is the case, write a comment and I will expand my answer.

For reading: http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/

thriqon
  • 2,458
  • 17
  • 23
  • I have to add that my model consists solely of orders. That custom date I put in front is my own implementation to show the data as shown in the image. (I will add this information to the question too) – DelphiLynx Dec 19 '14 at 08:56