Pergunta

I am trying to pass in converters and/or templates only to a specific template. According to the API you can only pass in helpers, but not converters or templates.

Is there any way to do this or does someone know if it is planned to support this in the future?

Note Passing them in globally via $.views.templates({...}) or $.views.converters({...}) is not really an option, because I will have way to many and perhaps even name-conflicting templates and converters.

Foi útil?

Solução

You can declare converters with your templates - and they will be private to the template. See Registering templates: $.templates(). Look for "Advanced scenarios: Associating private resources with templates".

In addition, the API for registering converters: $.views.converters({...}) also allows you to register a converter (or a set of converters) either globally, or locally just for a specific template. See the section "Adding converters as private resources for a parent template". To make them local, or private, to a template, just pass in the template as last parameter in your converters() call.

So here is a template with its own special converter declared along with the template:

$.templates({
  myTemplate: {
    markup: "Use my converter {{myconv:name}}",
    converters: {
      myconv: function(val) { return myCalculatedValue; }
    }
  }
});

Now {{myconv:...}} is specific to myTemplate and won't be available elsewhere.

Now suppose I want to dynamically replace the "myconv", still just within myTemplate. I can add/change it at any time using the converters() API:

$.views.converters(
  "myconv",
  function(val) { return myNewUpdatedCalculatedValue; },
  $.templates.myTemplate // Only override it for myTemplate, not globally...
);

Here are some related links:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top