Shany Shany - 1 year ago 33
Javascript Question

Empty array model used in dynamic route



When transitioning to a dynamic route with an empty array as the model, it seems like Ember relates to it as

undefined
.

Thus, if this is the template:

{{#linkTo dynamic arrayProxy}}link{{/linkTo}}


and this is the route definition:

DynamicRoute = Ember.Route.create(function() {
model: function(params) {
return this.modelFor("parent").find(params.name);
},

serialize: function(model) {
return { name: model.get("name") };
}
});


when
arrayProxy
is either
[]
or
Ember.ArrayProxy.create(content: [])
,
after clicking on the link:

the model hook will not get called, of course.

The serialize hook will get
undefined
as the
model
, no matter that ArrayProxy is a valid Ember object with its own properties.

Does anyone know how to force ember handle empty arrays differently from
undefined
?

Answer Source

It was my mistake...
Apparently, the link was nested in the template under the following if block:

{{#if arrayProxy}}
  {{#linkTo dynamic arrayProxy}}link{{/linkTo}}
{{else}}      
  {{#linkTo dynamic}}link{{/linkTo}}
{{/if}}

Since arrayProxy is an empty ArrayProxy, the else part gets called and the second linkTo is rendered.
Hence, DynamicRoute's serialize hook gets undefined as the model argument.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download