1

I want to toggle a single form (see picture below) using views. My code basically works when i add new formset it adds an empty bracket to json and then i print the json out using eachloop

View template

{{#each view.anotherField}}

<div class="panel panel-default">
     {{action 'toggleView' 'toggleViews' target='view'}}
        ...
</div>       
{{#unless view.toggleViews}}
...content to toggle...
{{/unless}}

View controller??

actions: {
    showMoreFields: function(){
      this.get('anotherField').pushObject({name: ''});

,

toggleView: function(param){
      this.toggleProperty(param);
    }

enter image description here

In this given picture u can see ive toggled organization view to truth what i would like is to toggle only the clicked part not all of the forms. Is there a solution ?

Cheers,

Kristjan

1 Answer 1

1

If I understand correctly you need to handle events for specific parts/forms of your view. To achieve this there are at least three approaches,

1.Use the {{action}} helper passing the object you want to modify. Then in your function modify a property of that object and reflect that in your template e.g. toggle the form. Maybe in your case it could be something like ,

....
{{#each field in view.anotherField}}

<div class="panel panel-default">
     {{action 'toggleView' field target='view'}}
....

2.Make a sub view/template (e.g. SubFormView) to accomodate each of your forms and handle the event of toggle within this view. Then include this via the {{view}} helper within the template of your main view.

3.Use pure js DOM handling (no {{action}} helper) and call your ember components from there.

Example of approaches 1 and 3 can be found here, http://emberjs.jsbin.com/acUCocu/1

hbs

<script type="text/x-handlebars" data-template-name="index">
    <i>using <b>&#123;&#123;action&#125;&#125;</b> helper</i>
    <ul>
    {{#each color in model}}
      <li {{action 'test' color}}>{{color.name}}</li>
    {{/each}}
    </ul>

    <i>using pure js DOM event handling</i>
    <ul>
    {{#each color in model}}
      <li onclick="Ember.View.views[$(this).closest('.ember-view').attr('id')].controller.send('test2',this)">{{color.name}}</li>
    {{/each}}
    </ul>
  </script>

js

App = Ember.Application.create();

App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return colors;
  },
  actions:{
    test:function(param){
      alert('this is color:'+param.get('name')+" ("+param+")");
    },
    test2:function(elem){
      alert('from pure js, clicked element: '+elem);
      $(elem).css('color','red');
    }
  }
});

App.Color = Ember.Object.extend({
  name:null
});

var colors=[];
colors.pushObject(App.Color.create({name:'red'}));
colors.pushObject(App.Color.create({name:'green'}));
colors.pushObject(App.Color.create({name:'blue'}));
Sign up to request clarification or add additional context in comments.

3 Comments

@kristjan thanks, i'm glad it worked. Happy new year!
Melc, ive seen you given many very useful answers, i wonder if i could ask your help with hasMany relation ? I wud make jsbin n so on .
@kristjanreinhold thanks, but can you please create a new thread with your question and I as well as many others will assist you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.