Skip to content

Instantly share code, notes, and snippets.

@alebon
Created September 12, 2013 08:34
Show Gist options
  • Save alebon/6534513 to your computer and use it in GitHub Desktop.
Save alebon/6534513 to your computer and use it in GitHub Desktop.
AngularJS with Lift 2.5
angular.module('IndexApp').controller 'SampleController', ["$scope", "sampleControllerBackend"
(scope, sampleControllerBackend) ->
scope.name = "Unknown user"
scope.alertSomething = ->
alert(sampleControllerBackend.update)
sampleControllerBackend.update(scope.name)
scope.$on 'ng-processing-error', ->
alert("Can't process")
]
...
<div data-lift="SampleControllerBackend.serverFunctions"></div>
...
<div class="lift:surround?with=layout/default;at=content" ng-app="IndexApp">
<header class="row">
<div class="large-12 columns">
<h1 class="subheader">Welcome</h1>
</div>
</header>
<div class="row" data-lift="SampleControllerBackend.allowModule" ng-controller="SampleController">
<div class="large-12 columns">
<label>Name:</label>
<input type="text" ng-model="name" placeholder="Enter a name here">
<hr>
<h1>Hello {{name}}!</h1>
<button ng-click="alertSomething()"></button>
</div>
</div>
</div>
class SampleControllerBackend extends StatefulSnippet with Logger {
protected lazy val controllerId = Helpers.nextFuncName
def dispatch = {
case "serverFunctions" => serverFunctions _
case "allowModule" => allowModule _
}
def allowModule(xhtml: NodeSeq): NodeSeq = {
<head>{Script(JsRaw("""var sampleControllerIdentifier = """" + controllerId + """"""").cmd)}</head> ++
xhtml
}
/**
* Creates a shared service to communicate to the backend
*
* @param xhtml
* @return
*/
def serverFunctions(xhtml: NodeSeq): Elem = {
val databaseHandler = new ListHandler(this)
val jsService = JsRaw("""
indexApp.factory('sampleControllerBackend', function() {
var s = {};
s.update = """ + databaseHandler.jsCmd.toJsCmd + """;return s;
});""".stripMargin).cmd
<div>{Script(jsService)}</div>
}
def doSave(jValue: JValue): JsCmd = {
debug(jValue)
JsCmds.Noop
}
/**
* Create/Update handler
*
* @param controller
*/
sealed class ListHandler(controller: SampleControllerBackend) extends JsonHandler {
implicit val formats = DefaultFormats.lossless
def apply(in: Any): JsCmd = in match {
case s:String => debug("Got string value")
val json: JValue = JString(s)
controller.doSave(json)
//ProcessingError("Can't process")
case x => debug(x.toString)
ProcessingError("Can't process %s".format(x.toString))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment