Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4840

Push product into an array model, controller

$
0
0

@chiholiu wrote:

How could I push an item into an array in this case.

//controlle/product.js

import Controller from ‘@ember/controller’;

export default Controller.extend({
appName: “ShoppingCart”,

products: [],

action: {
        add() {
            this.get('products').pushObject(this.get('product'));
        }
    }

});

//template/product.js

I am Product

{{appName}}

{{#each model as | product|}}

{{product.title}}

{{product.owner}}

{{product.city}}

<button {{action 'add' product}}>Add</button>
{{/each}}
    {{#each products as |product index| }}
  • {{product}}(remove)
  • {{/each}}

{{outlet}}

//router/product.js import Ember from ‘ember’;

export default Ember.Route.extend({ model() { return [{

              id: 1,
              title: 'Grand Old Mansion',
              owner: 'Veruca Salt',
              city: 'San Francisco',
        }, {
              id: 2,
              title: 'Grand Old Mansion',
              owner: 'Veruca Salt',
              city: 'San Francisco',
        }, {
              id: 3,
              title: 'Grand Old Mansion',
              owner: 'Veruca Salt',
              city: 'San Francisco',
        }, {
              id: 4,
              title: 'Grand Old Mansion',
              owner: 'Veruca Salt',
              city: 'San Francisco',
        }];
  }

});

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4840

Trending Articles