1

Is it possible to apply MVC pattern to javascript?

Asaph
  • 159,146
  • 25
  • 197
  • 199
Sarfraz
  • 377,238
  • 77
  • 533
  • 578

3 Answers3

9

Of course it is.

For instance, you can take a look at JavaScript MVC.

And, on SO :

Community
  • 1
  • 1
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

MVC is a general concept that can be implemented by any language ...

Dapeng
  • 1,704
  • 13
  • 25
1

Use Ember.js

These are the three features that make Ember a joy to use:

  1. Bindings
  2. Computed properties
  3. Auto-updating templates

Bindings

Use bindings to keep properties between two different objects in sync. You just declare a binding once, and Ember will make sure changes get propagated in either direction.

Here's how you create a binding between two objects:

MyApp.president = Ember.Object.create({
  name: "Barack Obama"
});

MyApp.country = Ember.Object.create({
  // Ending a property with 'Binding' tells Ember to
  // create a binding to the presidentName property.
  presidentNameBinding: 'MyApp.president.name'
});

MyApp.country.get('presidentName');
// "Barack Obama"

Bindings allow you to architect your application using the MVC (Model-View-Controller) pattern, then rest easy knowing that data will always flow correctly from layer to layer.

Computed Properties

Computed properties allow you to treat a function like a property. Computed properties are useful because they can work with bindings, just like any other property.

Auto-updating Templates

Ember uses Handlebars, a semantic templating library. To take data from your JavaScript application and put it into the DOM, create a tag and put it into your HTML, wherever you'd like the value to appear:

<script type="text/x-handlebars">
  The President of the United States is {{MyApp.president.fullName}}.
</script>
Community
  • 1
  • 1
Sam Hasler
  • 12,344
  • 10
  • 72
  • 106