Ng-admin 0.6: Adding Support for Custom Types

François Zaninotto
François ZaninottoFebruary 25, 2015
#ng-admin#angular-js#oss

ng-admin 0.6, released today, adds a couple of backwards compatible features, including a major change to the Configuration API: Factories.

Up to now, you used to add fields to entities by directly instanciating Field objects:

app.config(function (NgAdminConfigurationProvider, Application, Entity, Field) {
    var admin = new Application('my admin');
    var post = new Entity('posts');
    post.listView().fields([
        new Field('title'),
        new Field('published_at').type('date'),
        new Field('body').type('wysiwyg')
    ]);
}

Starting with 0.6, you have to use the factory functions provided by NgAdminConfigurationProvider:

app.config(function (NgAdminConfigurationProvider) {
    var nga = NgAdminConfigurationProvider;
    var admin = nga.application('my admin');
    var post = nga.entity('posts');
    post.listView().fields([
        nga.field('title'),
        nga.field('published_at', 'date'),
        nga.field('body', 'wysiwyg')
    ]);
}

nga.field() takes two parameters (name and type); calling .type() on an existing field isn't supported anymore.

And references are fields, too. Instead of new Reference(), new ReferenceMany(), and new ReferencedList(), use nga.field(name, type) with the type reference, reference_many, and referenced_list:

// replace
post
  .listView()
  .fields([
    new ReferenceMany("tags").targetEntity(tag).targetField(nga.field("name")),
  ]);
comment.listView().fields([
  new Reference("post_id")
    .label("Post")
    .targetEntity(post)
    .targetField(nga.field("title").map(truncate)),
]);

// by
post.listView().fields([
  nga
    .field("tags", "reference_many")
    .targetEntity(tag)
    .targetField(nga.field("name")),
]);
comment.listView().fields([
  nga
    .field("post_id", "reference")
    .label("Post")
    .targetEntity(post)
    .targetField(nga.field("title").map(truncate)),
]);

The documentation and demo were updated to reflect this change.

Usage of the old configuration API (with direct calls to new Field(), new Reference(), new ReferenceMany(), and new ReferencedList()) is deprecated. Support for this old syntax will be removed in 0.7.

This change allows you to override the way a given field is rendered, and even to create new field types.

In addition, ng-admin 0.6 allows to filter Reference results:

nga.field('post_id', 'reference')
   .targetEntity(nga.entity('post'))
   .targetField(nga.field('title', 'string')))
   .filters({ 'is_published': true })

There are many other changes listed in the Changelog.

Did you like this article? Share it!