Skip to content

Instantly share code, notes, and snippets.

@dbachet
Last active May 16, 2022 16:32
Show Gist options
  • Save dbachet/a323c96d3c95f15aefd63a423454da41 to your computer and use it in GitHub Desktop.
Save dbachet/a323c96d3c95f15aefd63a423454da41 to your computer and use it in GitHub Desktop.
New Twiddle
import Component from '@glimmer/component';
export default class extends Component {
}
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from "@glimmer/tracking";
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@action
updateProductName(productId){
this.model.data.forEach((product) => {
if (product.id == Number(productId)) {
product.name = "boom";
}
});
}
@action
updatePercentage(commissionId){
this.model.data.forEach((product) => {
product.commissions.forEach((commission, i) => {
if (commission.id == Number(commissionId)) {
commission.percentage = 20;
commission.validatedPercentage = true;
}
});
});
}
}
import Route from '@ember/routing/route';
import { tracked } from "@glimmer/tracking";
class DistributorCommisionProduct {
constructor({id, name, commissions}) {
this.id = id;
this.name = name;
this.commissions = commissions;
}
@tracked id;
@tracked name;
@tracked commissions;
}
class DistributorCommission {
constructor({id, campaignName, paid, percentage, validatedPercentage}) {
this.id = id;
this.campaignName = campaignName;
this.paid = paid;
this.percentage = percentage;
this.validatedPercentage = validatedPercentage;
}
@tracked id;
@tracked campaignName;
@tracked paid;
@tracked percentage;
@tracked validatedPercentage;
}
export default class ApplicationRoute extends Route {
model(params) {
const data = [
{
id: 1,
name: "Product 1",
commissions: [
{
id: 1,
percentage: 5,
validatedPercentage: false
},
{
id: 2,
percentage: 6,
validatedPercentage: false
},
]
},
{
id: 2,
name: "Product 2",
commissions: [
{
id: 3,
percentage: 10,
validatedPercentage: false
},
{
id: 4,
percentage: 11,
validatedPercentage: false
},
]
},
]
let products = data.map((product) => {
let commissions = product.commissions.map((commission) => {
return new DistributorCommission({
id: commission.id,
campaignName: commission.campaign_name,
paid: commission.paid,
percentage: commission.percentage,
validatedPercentage: commission.validated_percentage})
})
return new DistributorCommisionProduct({
id: product.id,
name: product.name,
commissions: commissions
})
})
return { data: products };
}
};
{{#each @model.data as |product|}}
<div>
{{product.name}}
<button {{on "click" (fn this.updateProductName product.id)}}>Update product name</button>
</div>
<MyComponent @commissions={{product.commissions}} @updatePercentage={{this.updatePercentage}}/>
{{/each}}
{{#each @commissions as |commission|}}
<div style="margin-left: 10px">{{commission.percentage}} %</div>
<button {{on "click" (fn @updatePercentage commission.id)}}>update percentage</button>
{{/each}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment