Switching Edit and View mode for user data

Leave a Comment

We can edit and save user data and save the input from the user to the view.

Main container for the view.

Then we need to create a view and events for the view.

var ContactForm = Backbone.View.extend({
      el: '#contact-form-container',          // Wrapper of the view
      events: {
        'click .edit-button': 'editForm',    // Event for edit form 
        'click .save-button': 'saveForm',    // Event for save form
      },
});

View Template



Edit Template



Then we need templates for two different view and here we are using handlebars template as follows

We need an initialize method which will create the general view while the page is loading. For that we need a model also. So we can do that as follows.

var ContactForm = Backbone.View.extend({
    el: '#contact-form-container',          // Wrapper of the view
    events: {
       'click .edit-button': 'editForm',    // Event for edit form 
       'click .save-button': 'saveForm',    // Event for save form
    },
    initialize: function () {
        var self = this;
        // Initial model for the view .
        this.contactData = {
            name: 'Debasis Panda',
            email: 'imdebasispanda@live.com',
            message: 'Debasis Panda: This is my message. This is my message. 
                     This is my message. This is my message. 
                     This is my message. This is my message.'
        }
         

});

Then we need a method which will dynamically compile templates and create view.

var ContactForm = Backbone.View.extend({
    el: '#contact-form-container',          // Wrapper of the view
    events: {
        ...
    },
    initialize: function () {
        var self = this;
        // Initial model for the view .
        this.contactData = {
             name: 'Debasis Panda',
             email: 'imdebasispanda@live.com',
             message: 'Debasis Panda: This is my message. This is my message. 
                      This is my message. This is my message. 
                      This is my message. This is my message.'
       }

       this.getTemplate(this.saveTemlate);
       $(document).on('click', '.save-button', function (e) {
           e.preventDefault();
           self.saveForm();
       });
    },
    getTemplate: function (templateId) {
       var html;
       var self = this;
       var template = Handlebars.compile($('#' + templateId).html());
       html = template(self.contactData);
       $(this.el).html(html);

    },
    
});

Once the default view is loaded then we will have an edit button. When we click the edit button it should generate a edit mode for the view. We can show the edit template in a modal as follows.



var ContactForm = Backbone.View.extend({
    el: '#contact-form-container',          // Wrapper of the view
    events: {
        ...
    },
    initialize: function () {
        ...
    },
    getTemplate: function (templateId) {
        ...
    },
    // Load the edit template in the modal.            
    editForm: function () {
       this.el = '#edit-form-container';
       this.getTemplate(this.editTemplate);
       $('#contactModal').modal('show');
    },             

});

Once the edit template is loaded we need to bind the click event for the save button and we need to save the data in the model.

var ContactForm = Backbone.View.extend({
   el: '#contact-form-container',          // Wrapper of the view
   events: {
      ...
   },
   initialize: function () {
      ...
   },
   getTemplate: function (templateId) {
      ...

   },
   // Load the edit template in the modal.            
   editForm: function () {
      this.el = '#edit-form-container';
      this.getTemplate(this.editTemplate);
      $('#contactModal').modal('show');
   }, 
   saveData: function (callback) {
      self = this;
      $.each($('#contact-form').serializeArray(), function (_, form) {
          self.contactData[form.name] = form.value;
      });
   }           

});

Once the data is saved then we need to render the default view with the new data.

var ContactForm = Backbone.View.extend({
   el: '#contact-form-container',          // Wrapper of the view
   events: {
       ...
   },
   initialize: function () {
       ...
   },
   getTemplate: function (templateId) {
       ...

   },
   // Load the edit template in the modal.            
   editForm: function () {
       ...
            }, 
             
   // Load the save template in the page container when the data is saved
   saveForm: function () {
       self = this;
       self.el = '#contact-form-container';

       self.saveData(function () {
            self.getTemplate(self.saveTemlate);
            $('#contactModal').modal('hide');
       });

   },
   saveData: function (callback) {
       self = this;
       $.each($('#contact-form').serializeArray(), function (_, form) {
           self.contactData[form.name] = form.value;
       });

       if (callback) {
           callback();
       };
   }         

});
var contactForm = new ContactForm();

Here is the example below.



Thank you for visiting.

Read More

Custom upload button using CSS

Leave a Comment

Sometimes the default file input element looks odd or sometimes the requirement will be different. Here are some techniques how we can customize the file input.



Read More

CSS RESET

Leave a Comment

What is css reset ?

CSS Reset is nothing but a set of user defined css rules which over-rides the default rules of the web browsers.

Why we need?

There are number of web browsers and they render the elements differently. Default view of the document is different. You might have seen some elements like select buttons, input elements, tables behave differently in IE, Chrome and Mozilla. So our design will be different in these browsers. In addition to this we have different new HTML5 element which still not supported to all browsers and they may not give desired result while using them.

In order to make standard and unique view of the document we define a set of new rules for all browsers.


How to reset css ?

We need to define new css rules for all the elements. Such as margin values, padding values, border, font properties, colors, list properties,  etc.

  1. Body has a default margin so it make some gapping between the window and the document. For that we need to reset that.      
        body{ margin: 0; }
  1. We have some HTML5 block elements like header, footer, section, article, aside etc. They need to be behave like block elements in all browsers.
   
article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary {
 display: block;
}   

  1. <template></template> Template tag is meant to keep the content which should not be rendered in the page. While IE does not support this feature. So we have to make it hidden by custom css. Any element with hidden attribute should be hidden also.

[hidden], template{ display: none;}

  1. Bold and Strong tag should be bold by default.
   
         b, strong{ font-weight: bold; }
   
  1. Table cells have default spacing. That will not look good in all situations. So this need to be standardized.  

    table {
     border-collapse: collapse;
     border-spacing: 0;
    }

    td, th { padding: 0; }

And many more ……..
Here is an example how CSS reset is useful.


With and Without CSS

Problems 

CSS Reset may not be useful always. Sometimes the requirement may be different. Suppose you have applied the css reset for your application but for some part you need the default styles. Then you have to re-write the default properties on your own and this will be a rework. Let consider the example below.
Here the default properties are overridden by the css reset but actually we need them. In this situation it will be a headache for the developer. So its always a good idea to be cleared about the requirement before applying the reset.

Reference

  1. HTML5 Doctor CSS.

    Thank you for reading.
    Debasis 
Read More

Margin Collapse

Leave a Comment
Margin Collapse is not any CSS property but its a behaviour of block level elements. Top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the margins combined into it, a behavior known as margin collapsing.


In the above example the first paragraph is having the margin bottom 20px and second paragraph is having margin top 30px. So here the two margins will collapse into one.

So the gap between two paragraphs will be 30px.


Read More
Next PostNewer Posts Previous PostOlder Posts Home