kristjan reinhold
kristjan reinhold

Reputation: 2048

Ember building form with views. Transfering view data array to application controller and then saving it

Im trying to have a field where u can duplicate a field and add another data in it E.g a person has a job and some data related to the job, then theres a button to add another workplace

Which would end up like this: 1: Job.scientist in an university 2: Job.teacher in a school and in the end of the form submit button. Right now ive come up with this. No clue if im even doing this the right way.

HBS>{{#each view.anotherField}}
        {{view Ember.TextField valueBinding="view.message"}}
    {{/each}}
    <br/>
    <button {{action 'moreFields' target='view'}}> click for another field </button>
    <button {{action 'moreFields'}}> Save the thingy </button>

And the JS

App.ApplicationView = Ember.View.extend({
    message: '',
    anotherField: [{name: 'testname'}],
    content: [{inputvalues: 'test value'}],

    actions: {

      moreFields: function(){
          this.get('anotherField').pushObject({name: ''});
          //input contents
          var something = this.get('message');
          // input content array
          this.get('content').pushObject({name: something});
          // log array out on each click
          console.log(this.get('content'));

      }

    }

And here is the JSBIN : http://jsbin.com/iSUdiCaX/3/edit

Duplicating fields works nicely. ember creates fields with unique IDS. Also i can get the data inside the input fields and put it in an array, so this is pretty nice right:P Now there should be a way to open this array in application controller. HOW? And then save array one by one to model : which has a relation of hasMany ?? Not even sure if on the right path if

Cheers,

Kristjan

Upvotes: 0

Views: 129

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

From the view, you can get the controller, and send an action to it

this.get('controller').send('addPerson', content);

http://jsbin.com/iSUdiCaX/6/edit

Upvotes: 1

Related Questions