I am trying to access a parent (coupons) child (couponactivations) relationship to use within my promo-codes component. For example I am trying to call user.get('couponActivations').get('coupons') within the model RSVP hash from the route. The coupons I would later loop through within the component. Like I mentioned, I was hoping to be able to return the coupons from the route but something like below doesn't work.
// app/routes/dashboard/settings/index.js
model() {
let user = this.store.peekRecord('user', this.get('session.data.authenticated.id')),
couponActivations = user.get('couponActivations');
return Ember.RSVP.hash({
user: user,
couponActivations: couponActivations,
coupons: couponActivations.get('coupons')
});
}
I have been successful getting coupons within the initializer for the component but I fear that strays way too far from the ember pattern and makes my component 'data aware'.
Here is my index route:
// app/routes/dashboard/settings/index.js
import Ember from 'ember';
import config from 'web-frontend/config/environment';
export default Ember.Route.extend({
session: Ember.inject.service(),
model() {
let user = this.store.peekRecord('user', this.get('session.data.authenticated.id'));
return Ember.RSVP.hash({
user: user,
profileImage: user.get('profileImage'),
couponActivations: user.get('couponActivations')
});
}
});
Here is the coupon model:
// app/models/coupon.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
discountType: DS.attr('string'),
discountValue: DS.attr('number'),
couponActivations: DS.hasMany('couponActivation', { async: true })
});
Here is my CouponActivation model:
// app/models/coupon-activation.js
import DS from 'ember-data';
export default DS.Model.extend({
expiresAt: DS.attr('date'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
promoCode: DS.attr('string'),
user: DS.belongsTo('user'),
coupon: DS.belongsTo('coupon')
});
In my index.emblem file I am calling the component:
// app/templates/dashboard/settings/index.emblem
.container
.row
.col-xs-12.col-sm-12.col-md-8.col-md-offset-2
.row-divider
= dashboard/settings/promo-codes listerAccount=model.listerAccount savePromoCode='savePromoCode'
Here is my promo-code component:
// app/components/dashboard/settings/promo-codes.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
submit() {
this.sendAction('savePromoCode', this.get('promoCode'));
this.set('promoCode', null);
}
}
});
Here is the promo-code.emblem template:
// app/templates/components/dashboard/settings/promo-codes.emblem
h2 = t 'dashboard.settings.promo-codes.title'
form submit="submit"
fieldset.form-group class={ if (v-get promoCode 'isInvalid') 'has-danger' }
.input-group
= input type="text" value=promoCode class="form-control"
if (v-get promoCode 'isInvalid')
span.text-help = v-get promoCode 'message'
span.input-group-btn
button.btn.btn-sm.btn-primary role="button"
= t 'dashboard.settings.promo-codes.apply-code'
if listerAccount
.row.m-t-1
.col-xs-5
.text-xs-center
p = t 'dashboard.settings.promo-codes.current-credit'
p = format-number listerAccount.creditsBalance style='currency' currency='USD'
.col-xs-7
.text-xs-center
p = t 'dashboard.settings.promo-codes.current-free-rentals'
Aucun commentaire:
Enregistrer un commentaire