0
votes

I've got ember (1.0.0-pre2) view which has a form and a button. The form is bound to a view class which allows populating form inputs and gathering entered values. Now on button click I want to create a dto object from the form values and send it to controller. The button html is as follows

<input type="submit" value="Add" data-icon="plus" {{action addReport view.report target="controller"}} />

The view code is

ContentView: baseView.extend({
    task: null, //initialized when form is filled in
    report: function () {
        var task = this.get('task');
        if (task == null)
            return null;

        return reportModel.create({
            taskId: task.id
        });
    }.property()
})

The controller code is

DayController: ember.Controller.extend({
    addReport: function (evt) {
        console.log(evt.context);
    }
})

When I run the code, the event context is null. When I look in the debugger, the report computed property executed only once, when the view is rendered. On button click the computed property is not evaluated - controller method is invoked right away.

Very similar construction works fine in the same application but for different view and the method is in the router, not controller:

<a href="#" {{action changeDate view.prevDate href=true}}>Prev</a>

view:

HeaderView: baseView.extend({
    prevDate: function () {
        return addDays(this.get('_dataSource').date, -1);
    }.property()
})

What is wrong with the button, why it doesn't work as opposed to the link?

2
Before I attempt to answer I just want to verify something: you have var task = this.get('task') in the report property function-- that means task is a property on ContentView in your code but just omitted from the example? - chrixian
Yes, task is the property of the ContentView. I updated the code in the question. - achekh

2 Answers

0
votes

Not sure if this is your specific problem but computed properties should specify dependencies. For example,

}.property('task')

Ember will cache computed properties. The dependency allows Ember to know when the cache property has been invalidated.

0
votes

Ok, after some debugging I found out the core misconception.

The context of the action evaluated by the handlebars action helper, which executed only when rendered. This was the reason why the other view worked - it didn't require the data provided by user in the current view, all necessary information was ready before the view displayed.

So the context field must be provided before the view is rendered ant this is not relevant for my scenario. I have to resort to the workaround in controller

DayController: ember.Controller.extend({
    addReport: function (evt) {
        console.log(evt.view.get('report'));
    }
})

Too bad, I hoped I could go without creating additional dependencies from controller to the view.