orionjs / orioncms

[Old] Orion is an open source framework built on Meteor that makes complex as well as simple apps possible with minimal effort.
http://orionjs.org
MIT License
715 stars 129 forks source link

Use/extend attributes in registerAttribute #363

Open macrozone opened 8 years ago

macrozone commented 8 years ago

its possible to create new attributes by extending other attributes. But i am not sure if this is the easiest way to do so, at least it is not totally "dry", I have to refer to the template of the original attribute:

E.g. create a attribute that enables to select 1-many posts and shows the option with an image of the post:

orion.attributes.registerAttribute("Posts", {
  template: orion.attributes.hasMany.template,
  getSchema() {
    return orion.attribute("hasMany", {}, {
      collection: Posts, titleField: "title", additionalFields: ["postImage"], publicationName: "attribute_Posts", render: {
        option(data, escape) {
          return `<div><img height="20" src="${escape(data.postImage.url)}"/> ${escape(data.title)}</div>`
        },
        item(data, escape) {
         return `<div><img height="20" src="${escape(data.postImage.url)}" /><br />  ${escape(data.title)}</div>`
       }
     }
   })
  }
});

It's pretty cool that this works out-of-the-box, but they may be a more generic way. :-)

nicolaslopezj commented 8 years ago

I think this is a great pattern for apps. I use a lot of attributes that are extensions of others, specially relationships.

macrozone commented 8 years ago

Agree, It works quite nice!

One issue is that meteor shows a warning because of duplicate of publishs when an attribute is used multiple times as the publicationName is the same.

Maybe we should create a wrapper-function that sets template and publicationName