scaleApp

scaleApp

mvc – very simple MVC

Estimated reading: 2 minutes 236 views

Here is a sample use case for using the MVC plugin (in coffeescript).

core = new scaleApp.Core
core.use scaleApp.plugins.mvc
core.boot()

class MyModel extends core.Model name: "Noname"

class MyView extends core.View

  constructor: (@model, @sandbox, @template) -> super @model

  # The render method gets automatically called when the model changes
  # The 'getContainer' method is provided by the dom plugin
  render: -> @sandbox.getContainer.innerHTML = @template @model

class MyController extends core.Controller

  changeName: (name) -> @model.set "name", name

core.registerModule "myModule", (@sandbox) ->

  init: (opt) ->

    # You can use any template engine you like. Here it's
    # just a simple function
    template = (model) -> "<h1>Hello #{model.name}</h1>"

    @m = new MyModel
    @v = new MyView @m, @sandbox, @template
    @c = new MyController @m, @v

    # listen to the "changeName" event
    @sandbox.on "changeName", @c.changeName, @c

  destroy: ->
    delete @c
    delete @v
    delete @m
    @sandbox.off @

core.emit "changeName", "Peter"

Share this Doc

mvc – very simple MVC

Or copy link

CONTENTS