Giter Club home page Giter Club logo

sails-viewify's Introduction

sails-viewify

npm npm npm Gitter chat

sails-viewify is a tool for reusing your sails models to build your sails views rapidly.It is specifically suited for building web apps that often involve generating HTML forms.

NOTE : sails-viewify is a helper module for the sails project.If you don't know what it is , first go ahead and see sails.

Install

Install sails-viewify from npm.

npm install -g sails-viewify

Example

Let us consider that you have a model called User.

module.exports = {

  attributes: {
    fname : {
      type : 'string'
    },

    lname : {
      type : 'string'
    },

    salary : {
      type : 'integer'
    } 
  }

};

Your obvious need might be creating a view that collects/exhibits the data for the model. In this case , your view may look like

<form method="POST" action="/user/add"> 
    <!-- First Name -->
    <label for="fname" > First Name </label>
    <input type="text" name="fname" id="fname" />
    <!-- Last Name -->
    <label for="lname" > Last Name </label>
    <input type="text" name="lname" id="lname" />
    <!-- Salary -->
    <label for="salary" > Salary </label>
    <input type="number" name="salary" id="salary" />
</form>

In this case , you only have 3 fields , but think of a situation

  • where your model has about some 30 attributes.
  • where you use some more tags and CSS classes to follow a uniform design throughout your project

You may feel tired of copying and pasting a template of html ( in this case , a label and input tag ) repeatedly and modify its name,id and value attributes each time by refering to the attribute name in the model's file.

What if we write our model and try to generate these fields for us based on the type of the attribute. Yeah ! This is what sails-viewify does !

You could use frontend frameworks like angular.js to do this.But there may be cases where you want to write the html code manually in order to keep a check on some minor details. sails-viewify serves this purpose.

How To Use

Create and navigate to your sails project. Go ahead and create your models and controllers.

sails new myproject 
cd myproject
sails generate model User
sails generate controller User 

Install sails-viewify globally.

npm install -g sails-viewify

Initialize sails-viewify in the sails project by executing the following command.

sails-viewify init

This creates two files namely ,

  • config/sails-viewify.js - Has the configuration details.
  • viewify_input.txt - Refer sails-viewify escape(extended use) for usage.

Next step is configuring your sails-viewify.js config file.

module.exports = {

  template: [
    {
      type: 'text' ,
      htmltext: '<p>type of {{name}} is {{type}}</p>\n',
      specials: [
        {
          text: '{{name}}' ,
          replacer: 'name'
        },
        {
          text: '{{type}}' ,
          replacer: 'type'
        }
      ]
    }
  ]

};
  • template - [array] specifies templates to be used for different types of attributes. Note that the type refers to the types used in the model and are available within waterline.
    • type - [string] specifies the type of the attributes used in the model.
    • htmltext - [string] specifies the html text to be generated for the respective attribute type.
    • collection - [string] specifies the collection of the attributes used in the model.
    • beginhtmltext - [string] specifies the html text to be generated for the respective attribute collection.
    • endhtmltext - [string] specifies the html text to be generated for the respective attribute collection.
    • specials - [array] specifies the special text in the htmltext field to be replaced by the respective replacer object value.
      • text - [string] special text that is to be replaced by the values from the model.
      • replacer - [string] replacer object points to the property of the attribute's object in the model. By default , sails-viewify creates name property which equals the name of the attribute in the model.Other than this , all the properties of the attribute are available.

That's it ! You are ready to generate your view . go ahead and execute

sails-viewify modelname viewname
sails-viewify User UserDetails.ejs

Now, the view named UserDetails.ejs can be found in your views folder.

<p>type of fname is string</p>
<p>type of lname is string</p>
<p>type of salary is integer</p>
Extended Use

Lets say , you want to add an id field to each p tag.Then your htmltext in config/sails-viewify.js would look like

htmltext : "<p id="{{name}}">type of {{name}} is {{type}}</p>" //invalid 

The above seen snippet is invalid since the double quotation marks inside the html are not escaped.So the valid syntax would be

htmltext : "<p id=\"{{name}}\">type of {{name}} is {{type}}</p>" //valid 

sails-viewify provides the utility to quickly generate your escaped html strings. It can be done as follows

Write the usual html snippet and copy paste in viewify_input.txt and run

sails-viewify escape

The escaped string can be copied and pasted from viewify_output.txt.

Command List

  • sails-viewify
  • sails-viewify init
  • sails-viewify escape

Goals and Plans

The main scope of viewify is to speed up the frontend development by reusing the models defined for the backend. However , it doesn't stop with that.

Future release plan includes ,

  • standardizing the sails-viewify.js configuration file.
  • User Interface for generating the configuration file and doing almost everything.
  • Migrating towards GUI based environment for basic sails frontend development.
  • Video Demo for using sails-viewify.
  • Current version of sails-viewify includes the command line tool only . Efforts will be taken to build API for using it programmatically.
  • currently handles types . Support for models and collections.
  • specifying the type of element to be used in model itself.

Contribution

Contributions in any form are welcomed. Some of the areas that currently need help are documentation and writing tests.You are also welcomed to join this project for standardizing the already existing stuff and for implementing the plans mentioned above.

License

sails-viewify is licensed under The MIT License

sails-viewify's People

Contributors

scriptnull avatar yannbertrand avatar

Stargazers

 avatar

Watchers

 avatar

sails-viewify's Issues

Infinite loop when a collection model include the first model.

A remaining issue is that if you have a collection in your model and the collection model has an attribute with the initial model (relation many to many) you should have an infinite loop.

Example file : user.js

module.exports = {
    attributes: {
        name: {
            type: 'string'
        },
        pets: {
            collection: 'Pet',
            via: 'owner'
        },
    }
};

pet.js

module.exports = {
    attributes: {
        name: {
            type: 'string'
        },
        owner: {
            collection: 'User',
            via: 'pets'
        },
    }
};

If in the sails-viewify.js configuration file you have defined :

module.exports = {
    template: [{
            type: 'string',
            htmltext: '<p>{{name}}</p>',
            specials: [{
                text: '{{name}}',
                replacer: 'name'
            }]
 }, {
            collection: 'Pets',
            beginhtmltext: '<div id="{{name}}" >\n',
            endhtmltext: '</div>  ',
            specials: [{
                text: '{{name}}',
                replacer: 'name'
            }]
 }, {
            collection: 'User',
            beginhtmltext: '<div id="{{name}}">',
            endhtmltext: '</div>  ',
            specials: [{
                text: '{{name}}',
                replacer: 'name'
            }]
 }]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.