samedi 28 mars 2015

How to link or transition to a dynamic segment route from another dynamic segment route in ember?

I can't seem to use transitionToRoute from the url .../product/1 to go to the url .../product/2. It works just fine from the "parent" products resource route, but only once when going from "sibling".


I'm using transitionToRoute in an observer that listens for a property to change upon clicking. This entity that is clicked is in the parent "products" route. So when I'm at a singular product route, and click the products clickable thing, it doesn't compute properly.


I'm also using fixtures for my data!


Why not just use link-to? Well I'm simplifying my situation for the sake of brevity, but I'm trying to make a fancier UI that is based on observing a property that is changed (that property is a computed property of other properties that get changed individually with each clickable div)


I've read about the model not being fired, how i need to use the setupController hook. And if I'm using a slug instead of an id, the serializer.


I've played around with it, but I can't seem to get things working properly. I imagnie an example solution would be simple and short, but I can provide code and context if needed.


What the hell, here is some boilerplate code: (ES6 code ommitted for brevity and readability)


router



Router.map ->
@resource 'products', ->
@resource 'product { path: '/:type' }, ->
@route 'description'


products route



ProductsRoute = Ember.Route.extend
setupController: (controller, model) ->
controller.set('model', DS.store.findAll('product'))


products controller



PersonalitiesController = Ember.ArrayController.extend
factorOne: 'q'
factorTwo: 'a'

actions:
toggleFactorOne: ->
if this.get('factorOne') == 'q'
this.set('factorOne', 'w')
else if this.get('factorOne') == 'w'
this.set('factorOne', 'q')

toggleFactorTwo: ->
if this.get('factorTwo') == 'a'
this.set('factorTwo', 's')
else if this.get('factorTwo') == 's'
this.set('factorTwo', 'a')

type: (->
this.get('factorOne') + this.get('factorTwo')
).property('factorOne', 'factorTwo')

typeChanged: (->
this.transitionToRoute('product', type)
).observe('type')


product model



Product = DS.Model.extend
type: DS.attr('string')
description: DS.attr('string')

Product.reopenClass
FIXTURES: [
{ type: 'qa', description: 'stuff'}
{ type: 'qs', description: 'stuff'}
{ type: 'wa', description: 'stuff'}
{ type: 'ws', description: 'stuff'}

]


Now for the singular product stuff


product route



ProductRoute = Ember.Route.extend
model: DS.store.query('product', params.type)

setupController: (controller, model) ->
modelCollection = DS.store.query('product', params.type)
controller.set('model', modelCollection.then (pers) ->
pers.get('firstObject'))

serialize: (product) ->
{ product: product.get('type') }


^ I think the product route is where the most problems lie.


product route


(nothing special here. empty)


Product adapter



ProductAdapter = DS.FixtureAdapter.extend()


I guess it wasn't that short. I feel I may be missing something with the model hooks, the code within the setupController hook, or serializing.


Thank you. Again, the symptoms are:


1) the url doesn't update when transitioning from a product route to another product route that has a different dynamic segment, only the first time.


2) the model is not set for the product route or from the product/description route.





Aucun commentaire:

Enregistrer un commentaire