Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember.js changing a view from another view

I have a show view that displays the selected item from a list. Then when i click on the edit button it displays the edit view via {{#if isEditing}}

When I click another item in the list the display view changes to the new selected item but stays in the isEditing state.

How can I change the isEditing state of a view from another view ?

I have done this at the moment with a FocusOut function that sets isEditing to false but I have 2 text fields in this view and so when I click into the other text field it triggers the focusOut function as well.

This must be simple but can't seem to figure it out!

like image 866
Rick Moss Avatar asked Feb 02 '12 18:02

Rick Moss


People also ask

What is the use of a view in Ember JS?

A view in an Ember.js is used for to handle user events and also creates the re-usable component. The UI will be developed by using the views. The below figure shows how event handling occurs −

How to use Handlebars templates in Ember JS?

Handlebars templates in Ember.js are powerful and can be rendered by using Ember.View and inserted into DOM. You can set templateName property of a view to indicate which template to use.

What is event handling in Emberjs?

When user clicks on the event the view turns a primitive event (a click) into a semantic event ( Event Handling) and transferred to the Ember.Route for to get the event action. Handlebars templates in Ember.js are powerful and can be rendered by using Ember.View and inserted into DOM.

How do I change the class name of a view?

A view is represented by a single DOM element on the page. You can change what kind of element is created by changing the tagName property. You can also specify which class names are applied to the view by setting its classNames property to an array of strings:


2 Answers

Nice question. I think you need to do something a little more complicated than a simple binding to drive the content of the App.SelectedItemView. I would try another computed property instead:

App.SelectedItemView = Ember.View.extend({
  isEditing: false,
  content: function() {
    var selectedItem = App.SelectedItemController.get('content');
    this.set('editingItem', false);
    return selectedItem;
  }.property('App.SelectedItemController.content'),
});

The trick about computed properties is that you can accomplish the same thing as a simple binding, but you can also add custom code to execute when the observed value changes (like conditionally setting editingItem to null in this case). The downside to a computed property is that it's a little more complicated to do a two-way binding (e.g. in this case to set App.SelectedItemController.content whenever App.SelectedItemView.content changes)--but it sounds like you don't need to do that anyway.

like image 170
Jeff Terrell Ph.D. Avatar answered Nov 06 '22 16:11

Jeff Terrell Ph.D.


How can i change the isEditing state of a view from another view ?

I don't think this is really want you want to do. In the click event for the item you are probably changing the content property of the controller. You want isEditing to be false whenever the content changes.

You can set up an observer to handle this:

App.SelectedItemView = Ember.View.extend(
{
    isEditing: false,
    contentBinding: 'App.SelectedItemController.content',
    contentChanged: function ()
    {
        this.set('isEditing', false);
    }.observes("content")
});
like image 24
Jason P Avatar answered Nov 06 '22 16:11

Jason P