How to render razor view in aurelia custom element? - asp.net-mvc

I am successfully rendering mvc view when it comes to render the normal aurelia class as below:
export class App {
getViewStrategy() {
return 'app';
}
}
How would I render my razor view when using custom element basic example is I got a nav-bar.html which is getting use to render my menu but instead of nav-bar.html I want to use cshtml page so that I can use some server side code to hide and show few elements. If I just do following
<template>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
....
</nav>
</template>
And in corresponding nav-bar.ts
import {bindable} from 'aurelia-framework';
export class NavBar {
#bindable router;
constructor() {
console.info("Ctor");
}
bind() {
console.info("bind");
}
getViewStrategy() {
console.log('get view');
return 'nav-bar'
}
}
And use it like, in my app.cshtml. One thing to note if I change from="nav-bar" to nav-bar.cshtml and get rid of nav-bar.ts file aurelia expect nav-bar.js all the time as it only care for js and html file
<template>
<require from="nav-bar"></require>
<require from="bootstrap/css/bootstrap.css"></require>
<nav-bar router.bind="router"></nav-bar>
<div class="page-host">
<router-view></router-view>
</div>
</template>
In this case my Ctor and bind is logging but get view doesn't which mean i cannot use server side view or is there any way around it?

Use the #useView decorator.
navbar.js
import { useView } from 'aurelia-framework';
#useView('navbar.cshtml')
export class NavBarCustomElement {
// ...
}
app.html
<require from="nav-bar"></require>
<nav-bar></nav-bar>
<div class="page-host">
<router-view></router-view>
</div>
Working example here: https://gist.run/?id=8f3b972d4008f07c1a11142b4b5b6e0e

Related

Angular Dart 2 - querySelect returning null

I'm trying to set up a drag&drop component to upload multiple files. However, when I attempt to access elements on the DOM with the querySelector method, I end up with null.
I've tried to implement the AfterViewInit class to no avail. Here's my current dart code for the component:
import 'dart:html';
import 'package:dnd/dnd.dart';
import 'package:angular/angular.dart';
#Component(
selector: 'upload',
templateUrl: 'upload.html',
styleUrls: [
'upload.css'
]
)
class Upload implements AfterViewInit {
#override
void ngAfterViewInit() {
// TODO: implement ngOnInit
Draggable d = new Draggable(document.querySelectorAll('.page'), avatarHandler : new AvatarHandler.clone());
var del = document.querySelector('.upload');
print(del); //prints null
Dropzone dropzone = new Dropzone(document.querySelector('.upload')); //throws an error, as it doesn't expect null.
dropzone.onDrop.listen((DropzoneEvent event){
print(event);
});
}
}
Also, my upload.html file is as follows:
<div class="center-me page" uk-grid>
<div class="uk-align-center text-align-center">
<h2 class="text-align-center" >Upload a file</h2>
<div class="upload uk-placeholder uk-text-center">
<span uk-icon="icon: cloud-upload"></span>
<span class="uk-text-middle">Attach binaries by dropping them here or</span>
<div uk-form-custom>
<input type="file" multiple>
<span class="uk-link">selecting one</span>
</div>
</div>
<progress id="progressbar" class="uk-progress" value="0" max="100" hidden></progress>
</div>
</div>
Thanks in advance.
So this looks like it should work. I wouldn't actually suggest doing it this way as it will get any element with an upload class which if you reuse the component will be a lot.
I would suggest using the ViewChild syntax instead
class Upload implements AfterViewInit {
#ViewChild('upload')
void uploadElm(HtmlElement elm) {
Dropzone dropzone = new Dropzone(elm);
dropzone.onDrop.listen((DropzoneEvent event){
print(event);
});
}
}
In the template:
<div class="uk-placeholder uk-text-center" #upload>
That said you shouldn't be getting null from the querySelect, but from the code you have shown I'm not sure why.

Angular 2 : list is not refresh on running IOS

I work in Angular 2 project (and also use Ionic 2).
In my project, I have a page to display pictures-list.
User can add/remove pictures (by cordova-camera plugin).
My problem is: when user remove picture, I remove it from list behind UI.
Debugging at chrome - work nice.
BUT, try emulate on IOS, or really test on Smart-phone, when user delete picture the view is doesn't get refresh till he press any button.
What should I do???
Here is my code:
HTML:
<ion-list>
<ion-col width-50 *ngFor="let picture of pictures">
<div>
<button (click)="checkAsGood(picture)">I like</button>
<button (click)="deletePicture(picture)"><ion-icon name="trash"></ion-icon></button>
</div>
<div>
<img [src]="picture.src" />
</div>
</ion-col>
</ion-list>
Java Script:
private deletePicture(pictureRecord:Picture) {
var self = this;
self.pictureService.deleteUserPicture(pictureRecord).then(function deleteSucceeded() {
self.pictures.splice(self.pictures.indexOf(pictureRecord), 1);
}, function deleteFaild(error) {
self.messagesService.showToastMessage(error.code)
});
}
Call change detection explicitly
class MyComponent {
constructor(private cdRef:ChangeDetectorRef) {}
private deletePicture(pictureRecord:Picture) {
this.pictureService.deleteUserPicture(pictureRecord).then(() => {
this.pictures.splice(this.pictures.indexOf(pictureRecord), 1);
this.cdRef.detectChanges();
}, (error) => {
this.messagesService.showToastMessage(error.code)
});
}
}
or ensure the callback is executed in Angulars zone already in the service using zone.run(...).
It looks like some functionality your pictureService is using functionality that isn't fully covered by the zone.js package on IOS.

Angular Dart: Data binding doesn't work when manipulating the controller from the outside, part two

A short background:
This example is a slightly more complicated version of my Angular Dart: Data binding doesn't work when manipulating the controller from the outside question that has been answered correctly. I only added a toggleable "show resolved comments" link to this version. Even though I initialized every variable to non-null values the problem still happens.
Full description of the actual problem:
I have two controllers nested into each other. The outer controller shows/hides the inner controller by using an ng-switch directive.
The outer controller also contains a checkbox. If this checkbox gets checked then the inner controller is made visible (via the above ng-switch directive). This checkbox works as intended.
There's also an "open" link outside the controllers. Its onclick handler calls into the outer controller and is supposed to check the checkbox via the model. The problem is that even though the model gets changed, the view doesn't get updated, unless I explicitly call scope.apply(), which I shouldn't. Even if I remove the comment before scope.apply() in my code then data binding doesn't work within InnerController.
This pattern has worked flawlessly in AngularJS but apparently doesn't in AngularDart.
I insist to this pattern or something similar because I'm in the process of integrating AngularDart into a legacy application that doesn't use data binding so I have to trigger model changes from outside the models.
Thanks in advance!
<html ng-app>
<head>
<title>Angular.dart nested controllers</title>
</head>
<body>
open
<div outer-controller ng-switch="outerCtrl.showInnerController">
<input type="checkbox" ng-model="outerCtrl.showInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
<div ng-switch="innerCtrl.showResolvedComments" style="text-decoration:underline; color:blue; cursor:pointer">
<div ng-switch-when="true" ng-click="innerCtrl.showResolvedComments = false">Hide resolved comments</div>
<div ng-switch-when="false" ng-click="innerCtrl.showResolvedComments = true">Show resolved comments</div>
</div>
</div>
<div inner-controller ng-switch-when="false">
other controller
</div>
</div>
<script type="application/dart">
import "dart:html";
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';
OuterController outerController;
#Controller(selector:'[outer-controller]', publishAs:'outerCtrl')
class OuterController {
bool showInnerController = false;
Scope scope;
OuterController(this.scope) {
outerController = this;
}
void showOuterController() {
showInnerController = true;
//scope.apply();
}
}
#Controller(selector:'[inner-controller]', publishAs:'innerCtrl')
class InnerController {
String yourName = 'defaultName';
bool showResolvedComments = true;
}
class MyAppModule extends Module {
MyAppModule() {
type(InnerController);
type(OuterController);
}
}
main() {
applicationFactory().addModule(new MyAppModule()).run();
querySelector('#open').onClick.listen((Event event) {
outerController.showOuterController();
});
}
</script>
</body>
</html>
After some experimentation, it's look like angular listen specified event to activate ng-model, and it doesn't look every variable change, i think because it's complicated to watch every change in variable without impact performance.
You can change your approach by simulate a user click on the check box
like:
CheckboxInputElement checkBox = querySelector("input");
if (checkBox.checked == false) {
checkBox.click();
}
It's maybe not the cleaner way to do this, but it works
Here the full code with the patch
<html ng-app>
<head>
<title>Angular.dart nested controllers</title>
</head>
<body>
open
<div outer-controller ng-switch="outerCtrl.showInnerController">
<input type="checkbox" ng-model="outerCtrl.showInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
<div ng-switch="innerCtrl.showResolvedComments" style="text-decoration:underline; color:blue; cursor:pointer">
<div ng-switch-when="true" ng-click="innerCtrl.showResolvedComments = false">Hide resolved comments</div>
<div ng-switch-when="false" ng-click="innerCtrl.showResolvedComments = true">Show resolved comments</div>
</div>
</div>
<div inner-controller ng-switch-when="false">
other controller
</div>
</div>
<script type="application/dart">
import "dart:html";
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';
OuterController outerController;
#Controller(selector:'[outer-controller]', publishAs:'outerCtrl')
class OuterController {
bool showInnerController = false;
Scope scope;
OuterController(this.scope) {
outerController = this;
}
void showOuterController() {
showInnerController = true;
print("showOuterController");
//scope.apply();
}
}
#Controller(selector:'[inner-controller]', publishAs:'innerCtrl')
class InnerController {
String yourName = 'defaultName';
bool showResolvedComments = true;
}
class MyAppModule extends Module {
MyAppModule() {
type(InnerController);
type(OuterController);
}
}
main() {
applicationFactory().addModule(new MyAppModule()).run();
querySelector('#open').onClick.listen((Event event) {
outerController.showOuterController();
// Added Code
CheckboxInputElement checkBox = querySelector("input");
if (checkBox.checked == false) {
checkBox.click();
}
// End added code
});
}
</script>
</body>
</html>

When do I need to call watchers.dispatch() in Dart?

I have a small application I am building that is very similar to the example here.
I am using Dart SDK version 0.5.9.0_r22879
The main difference is that I update the results via an AJAX request, and I only make this request when Enter is pressed in my input control.
In my code, the results list does not render unless I explicitly call watchers.dispatch(), as discussed in the 2nd example here.
Why? It is not clear when I would have to explicitly call watchers.dispatch(), and when it would happen automatically, as in the template example.
My HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My App</title>
<link rel="stylesheet" href="myapp.css">
</head>
<body>
<h1>My App</h1>
<div id="seach-box-container">
<input type="text" name="search-box" id="search-box" placeholder="Search" bind-value="searchText" />
</div>
<div id="results-container">
<template instantiate="if noMatches"><span>No matches</span></template>
<template instantiate="if !noMatches"><span>{{results.length}} entries:</span></template>
<div id="app-entries">
<ul>
<template iterate='entry in results'>
<li><pre>{{entry.message}}</pre></li>
</template>
</ul>
</div>
</div>
<script type="application/dart" src="myapp.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
The important parts of myapp.dart:
import 'dart:html';
import 'dart:json' as JSON;
import 'dart:uri' as uri;
import 'package:web_ui/web_ui.dart';
import 'package:web_ui/watcher.dart' as watchers;
String searchText = '';
List<LogEntry> results = [];
bool get noMatches => results.isEmpty;
void main() {
query("#search-box").onKeyPress.listen((e) => handleKeyPress(e));
}
void handleKeyPress(KeyboardEvent e) {
if (!e.ctrlKey && e.keyCode == KeyCode.ENTER) {
doSearch();
}
}
void doSearch() {
if (searchText != '') {
makeRequest();
}
}
void makeRequest() {
HttpRequest.getString( 'http://url.to/rest-api?q=$searchText' )
.then(processString)
.catchError(handleError)
;
}
processString(String jsonString) {
List<Map> logs = JSON.parse(jsonString);
results.clear();
results.addAll( logs.map((l) => new AppEntry.fromJson(l)) );
watchers.dispatch();
}
handleError(Error error) {
print('Request failed');
print(error);
}
class AppEntry {
final String message;
AppEntry.fromJson(Map json) : message = json['message'];
}
You need to call watchers.dispatch() explicitly whenever you need to change the model in a way which is not triggered by events fired by the templates. This means AJAX calls like in your example, timers, etc.
Events installed by templates take care of calling dispatch() for you, so you don't have to do it in that case.
You can find more about this here.
However, at the moment, watchers.dispatch is treated as 'old way', as one of the goals in Web-UI is to make binding more declarative with observables. So the future-proof solution would be to use #observable annotation on your model. This will ensure that observers are updated every time the model changes, without needing you to explicitly update them.

Multiple ViewModels with Knockout and ASP.NET MVC4 SPA

I'm new to ASP.NET MVC SPA and Knockout.js os maybe it's a simple mistake I made...
Situation: I have two partialviews in my website and I want that every partialview has his own Knockout ViewModel so I won't get a huge ViewModel.
My current ViewModel:
/// <reference path="../_references.js" />
function MobileDeliveriesViewModel() {
var self = this;
// Data
self.currentDelivery = ko.observable();
self.nav = new NavHistory({
params: { view: 'deliveries', deliveryId: null }
});
// Test
self.foo = "FooBar"
self.bar = "BarFoo"
self.nav.initialize({ linkToUrl: true });
// Navigate Operations
self.showDeliveries = function () { self.nav.navigate({ view: 'deliveries' }) }
self.showCustomers = function () { self.nav.navigate({ view: 'customers' }) }
}
function BarFooViewModel() {
var self = this
//MobileDeliveriesViewModel.call(self)
self.bar2 = "BarFooTwo"
}
ko.applyBindings(new MobileDeliveriesViewModel());
ko.applyBindings(new MobileDeliveriesViewModel(), $('#BarFoo')[0]);
ko.applyBindings(new BarFooViewModel(), document.getElementById('BarFoo'));
My Index.cshtml:
<div data-bind="if: nav.params().view == 'deliveries'">
#Html.Partial("_DeliveriesList")
</div>
<div class="BarFoo" data-bind="if: nav.params().view == 'customers'">
#Html.Partial("_CustomersList")
</div>
<script src="~/Scripts/App/DeliveriesViewModel.js" type="text/javascript"></script>
My CustomerPartialView:
<div id="BarFoo" class="content">
<p data-bind="text: bar"></p>
<p data-bind="text: bar2"></p>
<button data-bind="click: showDeliveries, css: { active: nav.params().view == 'deliveries' }">Deliveries</button>
</div>
My DeliveriesPartialView:
<div class="content">
<p data-bind="text: foo"></p>
<button data-bind="click: showCustomers, css: { active: nav.params().view == 'customers' }">Customers</button>
</div>
If I run this, it won't recognize the bar2 propertie in my BarFooViewModel...
I have tried 2 different applyBindings at the end of my ViewModel.
Anybody got an idea or is their a better way/pattern to do this?
are there JS errors on page?
nav.params().view
but params: { view: 'deliveries', deliveryId: null } - it's not function.
and if you want use a few view models on single page - check this http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+KnockMeOut+%28Knock+Me+Out%29 acticle. you have to use "stopBinding"
It looks like you are applying multiple data bindings to the same sections.
ko.applyBindings(new MobileDeliveriesViewModel();
This will bind to all elements one the page.
ko.applyBindings(new MobileDeliveriesViewModel(), $('#BarFoo')[0]);
this will try to bind to all elements inside the div
ko.applyBindings(new BarFooViewModel(), document.getElementById('BarFoo'));
This will also try to bind to all elements inside the div.
To keep things simple, you should try to bind a single view model to a single html section. I've found that trying to bind two view models in the same html section has been hard to get work correctly and trouble shoot.
Jack128's answer also makes some good points.

Resources