How to architecture a webapp using jquery-mobile and knockoutjs - mvvm

I would like to build a mobile app, brewed from nothing more but html/css and JavaScript. While I have a decent knowledge of how to build a web app with JavaScript, I thought I might have a look into a framework like jquery-mobile.
At first, I thought jquery-mobile was nothing more then a widget framework which targets mobile browsers. Very similar to jquery-ui but for the mobile world. But I noticed that jquery-mobile is more than that. It comes with a bunch of architecture and let's you create apps with a declarative html syntax. So for the most easy thinkable app, you wouldn't need to write a single line of JavaScript by yourself (which is cool, because we all like to work less, don't we?)
To support the approach of creating apps using a declarative html syntax, I think it's a good take to combine jquery-mobile with knockoutjs. Knockoutjs is a client-side MVVM framework that aims to bring MVVM super powers known from WPF/Silverlight to the JavaScript world.
For me MVVM is a new world. While I have already read a lot about it, I have never actually used it myself before.
So this posting is about how to architecture an app using jquery-mobile and knockoutjs together. My idea was to write down the approach that I came up with after looking at it for several hours, and have some jquery-mobile/knockout yoda to comment it, showing me why it sucks and why I shouldn't do programming in the first place ;-)
The html
jquery-mobile does a good job providing a basic structure model of pages. While I am well aware that I could have my pages to be loaded via ajax afterwards, I just decided to keep all of them in one index.html file. In this basic scenario we are talking about two pages so that it shouldn't be too hard to stay on top of things.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="libs/jquery-mobile/jquery.mobile-1.0a4.1.css" />
<link rel="stylesheet" href="app/base/css/base.css" />
<script src="libs/jquery/jquery-1.5.0.min.js"></script>
<script src="libs/knockout/knockout-1.2.0.js"></script>
<script src="libs/knockout/knockout-bindings-jqm.js" type="text/javascript"></script>
<script src="libs/rx/rx.js" type="text/javascript"></script>
<script src="app/App.js"></script>
<script src="app/App.ViewModels.HomeScreenViewModel.js"></script>
<script src="app/App.MockedStatisticsService.js"></script>
<script src="libs/jquery-mobile/jquery.mobile-1.0a4.1.js"></script>
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="home">
<div data-role="header">
<h1>Demo App</h1>
</div><!-- /header -->
<div data-role="content">
<div class="ui-grid-a">
<div class="ui-block-a">
<div class="ui-bar" style="height:120px">
<h1>Tours today (please wait 10 seconds to see the effect)</h1>
<p><span data-bind="text: toursTotal"></span> total</p>
<p><span data-bind="text: toursRunning"></span> running</p>
<p><span data-bind="text: toursCompleted"></span> completed</p>
</div>
</div>
</div>
<fieldset class="ui-grid-a">
<div class="ui-block-a"><button data-bind="click: showTourList, jqmButtonEnabled: toursAvailable" data-theme="a">Tour List</button></div>
</fieldset>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<h4>by Christoph Burgdorf</h4>
</div><!-- /header -->
</div><!-- /page -->
<!-- tourlist page -->
<div data-role="page" id="tourlist">
<div data-role="header">
<h1>Bar</h1>
</div><!-- /header -->
<div data-role="content">
<p>Back to home</p>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<h4>by Christoph Burgdorf</h4>
</div><!-- /header -->
</div><!-- /page -->
</body>
</html>
The JavaScript
So let's come to the fun part - the JavaScript!
When I started to think about layering the app, I have had several things in mind (e.g. testability, loose coupling). I'm going to show you how I decided to split of my files and comment things like why did I choose one thing over another while I go...
App.js
var App = window.App = {};
App.ViewModels = {};
$(document).bind('mobileinit', function(){
// while app is running use App.Service.mockStatistic({ToursCompleted: 45}); to fake backend data from the console
var service = App.Service = new App.MockedStatisticService();
$('#home').live('pagecreate', function(event, ui){
var viewModel = new App.ViewModels.HomeScreenViewModel(service);
ko.applyBindings(viewModel, this);
viewModel.startServicePolling();
});
});
App.js is the entry point of my app. It creates the App object and provides a namespace for the view models (soon to come). It listenes for the mobileinit event which jquery-mobile provides.
As you can see, I'm creating a instance of some kind of ajax service (which we will look at later) and save it to the variable "service".
I also hook up the pagecreate event for the home page in which I create an instance of the viewModel that gets the service instance passed in. This point is essential to me. If anybody thinks, this should be done differently, please share your thoughts!
The point is, the view model needs to operate on a service (GetTour/, SaveTour etc.). But I don't want the ViewModel to know any more about it. So for example, in our case, I'm just passing in a mocked ajax service because the backend hasn't been developed yet.
Another thing I should mention is that the ViewModel has zero knowledge about the actual view. That's why I'm calling ko.applyBindings(viewModel, this) from within the pagecreate handler. I wanted to keep the view model seperated from the actual view to make it easier to test it.
App.ViewModels.HomeScreenViewModel.js
(function(App){
App.ViewModels.HomeScreenViewModel = function(service){
var self = {}, disposableServicePoller = Rx.Disposable.Empty;
self.toursTotal = ko.observable(0);
self.toursRunning = ko.observable(0);
self.toursCompleted = ko.observable(0);
self.toursAvailable = ko.dependentObservable(function(){ return this.toursTotal() > 0; }, self);
self.showTourList = function(){ $.mobile.changePage('#tourlist', 'pop', false, true); };
self.startServicePolling = function(){
disposableServicePoller = Rx.Observable
.Interval(10000)
.Select(service.getStatistics)
.Switch()
.Subscribe(function(statistics){
self.toursTotal(statistics.ToursTotal);
self.toursRunning(statistics.ToursRunning);
self.toursCompleted(statistics.ToursCompleted);
});
};
self.stopServicePolling = disposableServicePoller.Dispose;
return self;
};
})(App)
While you will find most knockoutjs view model examples using an object literal syntax, I'm using the traditional function syntax with a 'self' helper objects. Basically, it's a matter of taste. But when you want to have one observable property to reference another, you can't write down the object literal in one go which makes it less symmetric. That's one of the reason why I'm choosing a different syntax.
The next reason is the service that I can pass on as a parameter as I mentioned before.
There is one more thing with this view model which I'm not sure if I did choose the right way. I want to poll the ajax service periodically to fetch the results from the server. So, I have choosen to implement startServicePolling/stopServicePolling methods to do so. The idea is to start the polling on pageshow, and stop it when the user navigates to different page.
You can ignore the syntax which is used to poll the service. It's RxJS magic. Just be sure I'm polling it and update the observable properties with the returned result as you can see in the Subscribe(function(statistics){..}) part.
App.MockedStatisticsService.js
Ok, there is just one thing left to show you. It's the actual service implementation. I'm not going much into detail here. It's just a mock that returns some numbers when getStatistics is called. There is another method mockStatistics which I use to set new values through the browsers js console while the app is running.
(function(App){
App.MockedStatisticService = function(){
var self = {},
defaultStatistic = {
ToursTotal: 505,
ToursRunning: 110,
ToursCompleted: 115
},
currentStatistic = $.extend({}, defaultStatistic);;
self.mockStatistic = function(statistics){
currentStatistic = $.extend({}, defaultStatistic, statistics);
};
self.getStatistics = function(){
var asyncSubject = new Rx.AsyncSubject();
asyncSubject.OnNext(currentStatistic);
asyncSubject.OnCompleted();
return asyncSubject.AsObservable();
};
return self;
};
})(App)
Ok, I wrote much more as I initially planned to write. My finger hurt, my dogs are asking me to take them for a walk and I feel exhausted. I'm sure there are plenty things missing here and that I put in a bunch of typos and grammer mistakes. Yell at me if something isn't clear and I will update the posting later.
The posting might not seem as an question but actually it is! I would like you to share your thoughts about my approach and if you think it's good or bad or if I'm missing out things.
UPDATE
Due to the major popularity this posting gained and because several people asked me to do so, I have put the code of this example on github:
https://github.com/cburgdorf/stackoverflow-knockout-example
Get it while it's hot!

Note: As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
I'm working on the same thing (knockout + jquery mobile). I'm trying to write a blog post about what I've learned but here are some pointers in the meantime. Remember that I'm also trying to learn knockout/jquery mobile.
View-Model and Page
Only use one (1) view-model object per jQuery Mobile-page. Otherwise you can get problems with click-events that are triggered multiple times.
View-Model and click
Only use ko.observable-fields for view-models click-events.
ko.applyBinding once
If possible: only call ko.applyBinding once for every page and use ko.observable’s instead of calling ko.applyBinding multiple times.
pagehide and ko.cleanNode
Remember to clean up some view-models on pagehide.
ko.cleanNode seems to disturb jQuery Mobiles rendering - causing it to re-render the html. If you use ko.cleanNode on a page you need to remove data-role’s and insert the rendered jQuery Mobile html in the source code.
$('#field').live('pagehide', function() {
ko.cleanNode($('#field')[0]);
});
pagehide and click
If you are binding to click-events - remember to clean up .ui-btn-active. The easiest way to accomplish this is using this code snippet:
$('[data-role="page"]').live('pagehide', function() {
$('.ui-btn-active').removeClass('ui-btn-active');
});

Related

Can i use ngxErrors or something like it to display a form error?

I use ngxErrors to display errors for a form control and it works great. Is there any way to get similar functionality for a form or a form group? Currently, I display a form error like this:
<div *ngIf="form.hasError('loginFailed')">
Login Failed
</div>
The bummer is, when I detect that there is a form error (e.g. after the login form is submitted) as opposed to control error, I set it like this:
this.form.setErrors({ loginFailed: true });
this.cdr.detectChanges();
Where this.cdr is an instance of ChangeDetectorRef. This is necessary because I'm using OnPush change detection strategy. So basically it's like calling $scope.$apply() from AngularJS all over again.
What I would really like to do is something more like how ngxErrors does it:
<div ngxErrors="myForm">
<div ngxError="loginFailed" [when]="['dirty', 'touched']">
The login has failed
</div>
But ngxErrors expects myForm to be a control.
This feature is not currently baked into ngxErrors, but I submitted a PR. https://github.com/UltimateAngular/ngxerrors/pull/18
The working syntax is a slight modification of the above:
<div ngxErrors>
<div ngxError="loginFailed" [when]="['dirty', 'touched']">
The login has failed
</div>
</div>
I learned that you do not have to tell child components the form, the FormGroupDirective is available to children automatically.
See this library https://www.npmjs.com/package/ng-error-messages for show error messages based on validation rules:
<input placeholder="Texto:" formControlName="text">
<div errorMessage="text" alias="Super Texto" ></div>

Ng-include doesnt work in Sails Js

I have a problem with ng-include.
in my Layout.ejs I have de html ng-app
This is my code:
homepage.ejs:
<div class="slide">
<div class="container" ng-controller="slide">
<h1 class ="main-title"> equipo mate </h1>
<ng-include src ="'partial.html"></ng-include>
</div>
</div>
angular.js:
angular.module('app',[])
.controller('slide', function($scope){
});
and partial.html, that have "HEllo World"
This is the error: angular.js:12410GET http://localhost:1337/partial.html 404 (Not Found)
Somebody can help me?
AngularJS is client side framework.
<ng-include src ="'partial.html"></ng-include>
Which means, this line is executed in your browser
and Angular tries to get http://localhost:1337/partial.html to include it in view.
Now you have to ensure that partial.html is available at that (or some public path).
You should put partial.html inside assets folder for this.
EJS is rendered on server side (in this case).
Angular is rendered on client side.
You should have clarity on this when using both together.

v5.6.1 publish instance redirecting for certain files

I started up a clean AEM 5.6.1 publish instance and am trying to change the default admin password. I went to http://localhost:4503/libs/granite/security/content/useradmin.html, but the page does not load correctly. I'm able to tell that some files load correctly (jquery.js, utils.js), but other files (userpicker.css, userpicker.js) respond with a JavaScript redirect:
<html>
<head>
<script type="text/javascript">var u="/content/geometrixx/en/toolbar/account/login.html?resource=%2Flibs%2Fgranite%2Fui%2Fcomponents%2Ffoundation%2Fform%2Fuserpicker%2Fclientlibs%2Fuserpicker.css&$$login$$=%24%24login%24%24"; if ( window.location.hash) {u = u + window.location.hash;} document.location = u;</script>
</head>
<body>
<!-- QUICKSTART_HOMEPAGE - (string used for readyness detection, do not remove) -->
</body>
</html>
What's interesting is that although I can't access userpicker.css, I am able to navigate to userpicker/userpicker.css without any problems.
Can someone explain what's happening here, and what I need to do to correct this behavior?

How to Update asp:Label after AJAX Callback

I'm using the ajaxtoolkit:Rating. It all works fine except I'm trying to write a value to an asp label on the changed event and can't get it to work. Here are the relevant lines of code:
page.aspx
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<td>
<ajaxtoolkit:Rating ID="YourRating" runat="server" BehaviorID="RatingBehavior1" CurrentRating="0"
MaxRating="10" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar"
ReadOnly="false" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar"
OnChanged="YourRating_Changed" />
</td>
<td>
(<asp:Label ID="YourRatingNumber" Text="" runat="server"></asp:Label>)
</td>
</LoggedInTemplate>
<AnonymousTemplate>
<td>
Login or Register to Rate
</td>
</AnonymousTemplate>
page.aspx.cs
protected void YourRating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
((HtmlGenericControl)FindControl("MainContent_LoginView1_YourRatingNumber")).InnerHtml = e.Value;
}
What I'm trying to do is update my asp:label control named "YourRatingNumber" text to e.value. I've tried many ways. I realize the asp:label is rendered as a span tag but I can't seem to access that value either. How can do this?
Thanks ... Bob
AJAX is asynchronous, meaning that a page load does not occur. Changing the value of a label server-side during an AJAX operation causes nothing client side. You would need to signal the client to update the page. You should use javascript to do this. I do not think you can do this using Web Forms. If you use MVC, you can use their AJAX helpers to do this, but it would require rewriting the site.
I would suggest that you write some javascript to cause an AJAX event and have it update the label when it gets a response. I would also suggest reading http://w3schools.com/ajax/default.asp, it explains how to use javascript to create AJAX requests, but you would need to tweak it a little to work with asp.net.
Also note, you should include whether you are using Web Forms or MVC, although they are similar, they have different handling of AJAX.

Intercept WordPress Admin Dashboard for Alerts

How can I make a plugin send an alert to one's dashboard at the top right below where the WordPress upgrade notice might normally appear?
I imagine I need some kind of add_filter() or add_action() call in the plugins' code.
Insert the following code in either a functions.php of your theme, or in a plugin. It's a sample that you can adapt and take further. In this demo, I wanted to demonstrate where you could post a message saying that the user needs to update a plugin.
<? function addDashboardAlert() { ?>
<style type="text/css">
.alert {
padding-top:4px;
padding-bottom:6px;
padding-left:302px;
background-color:#ebfbff;
border-bottom:1px solid #CCC;
display:none;
}
</style>
<script type="text/javascript">
$j = jQuery;
$j().ready(function(){ //when page has fully loaded
$j('h2:contains("Dashboard")').parent().prev().after('<div id="my-plugin-alert" class="alert">X Plugin 2.0 is available. Upgrade Now!</div>');
setTimeout("$j('#my-plugin-alert').fadeIn('slow');clearTimeout();",1000);
});
</script>
<? } add_action('admin_head','addDashboardAlert'); ?>
First, it intercepts admin_head to insert some Javascript. In the Javascript, since we know that current WordPress's include jQuery, but loads it with the .noConflict() option, then we can assign the jQuery to $j to keep it short in our code. When the page has fully loaded, it looks for an H2 that contains "Dashboard", meaning the Dashboard page. It then traverses up the DOM a little to a nice slot where it can insert the alert and then adds one there. It concludes with a professional fadeIn() call.