Understanding Two-Way Data-Binding in AngularJS - data-binding

I'm new to AngularJS. A long time I tried to abuse it the way I've always used Javascript-Frameworks like JQuery or Mootools. Now I understood that it's not gonna work like that anymore... But I've come across some big problems since I always generate my HTML-output with a CMS.
So it's pretty static, when it first comes out... Small example:
<ul>
<li>foo <span>delete</span></li>
<li>bar <span>delete</span></li>
<li>blub <span>delete</span></li>
</ul>
Now I thought, that Two-Way Databinding means I can generate the View with help of the Angular Scope and Controller, but also can generate Models by the View.
I may got something confused there... So here's my question. Is there any way to initiate Models from static HTML-output from a CMS?
I tried something like this...
<ul ng-controller="Ctrl">
<li ng-init="item[0].name=foo">{{item[0].name}} <span ng-click="remove(0)">delete</span></li>
<li ng-init="item[1].name=bar">{{item[1].name}} <span ng-click="remove(1)">delete</span></li>
<li ng-init="item[2].name=blub">{{item[2].name}} <span ng-click="remove(2)">delete</span></li>
</ul>
And in my controller I wrote a delete function. But when it did delete, it did only delete the name... the span-button was still there
It did work though when I defined my data as an javascript-array and did the whole output via Angular with ng-repeat... like this:
<ul ng-repeat="it in item">
<li>{{it.name}} <span ng-click="remove($index)">delete</span></li>
</ul>
I hope I made a point here and everyone get's my dificulty and problems? Can anyone tell me if what I was trying there is possible at all?

This is a common issue people have adjusting to Angular and other frameworks like it.
You don't need your server to render the HTML for you anymore. All you need to do is set up the template, and load the proper data into the scope.
<ul ng-controller="Ctrl" ng-init="getMyItems()">
<li ng-repeat="item in items">{{item.name}} <a ng-click="remove($index)">delete</a></li>
</ul>
And in your controller you'd do something like this
app.controller('Ctrl', function($scope, $http) {
$scope.items = [];
$scope.getMyItems = function(){
$http.get('/my/json/stuff/url').success(function(data) {
$scope.items = data;
$scope.$apply();
});
};
});
Now I know you're probably thinking "but I don't want to make a seperate request to get my JSON. And that's fine (probably irrelevant, but fine)... All you need to do is stick it into a global variable and retrieve it with $window instead.

Let's talk code. Here is real time app which shows profile pictures which is bind to data from MySQL. When anything changes in MySQL ( model ) view (HTML) will be updated.
app.controller('two_way_control',function($scope,$http,$interval){
load_pictures();
$interval(function(){
load_pictures();
},300);
function load_pictures(){
$http.get('http://localhost:3000/load').success(function(data){
$scope.profile_pictures=data;
});
};
Here is HTML
<div id="container" ng-app='two_way' ng-controller='two_way_control'>
<div class="row" ng-repeat="data in profile_pictures">
<div class=".col-sm-6 .col-md-5 .col-lg-6">
<h4>User Say's</h4><hr>
<p>
This is a Demo feed. It is developed to demonstrate Two way data binding.
</p>
<img src="{{data.profile_picture}}">
</div>
</div>
</div>
Learn more :
http://codeforgeek.com/2014/09/two-way-data-binding-angularjs/
Hope it helps !

Related

add button is missing for Content:Toolbar

The add button that appears over the 2sxc items is missing all of a sudden. It was there a couple days agao but now when I log into any portal in my DNN instance the "+" or add button is missing
here is a screen shot:
As you can see, the change layout and edit buttons are there. Not sure why the add button disappeared.
This is true for apps that I import from the 2sxc.org website as well. So I know its not just my template becasue it also happens on all the apps I have created which use different templates.
But to be thorough, here is my template code, its token based:
<div class="kr-gallery animation">
<p>Hover or touch image and click brush icon for more details</p>
<div class="isotope_grid isotope_grid2">
<div class="isotope_main animation" data-min-width="230">
<repeat repeat="Content in Data:Default">
<div class="isotope_item kr-gallery-item sc-element">[Content:Toolbar]
<div class="photo"><a href="[Tab:FullUrl]/details/[Content:EntityId]"> <img alt="" src="[Content:Image]?h=500" />
<span class="fa fa-paint-brush"></span></a>
</div>
</div>
</repeat>
</div>
</div>
</div>
Any idea why this is?
UPDATE:
Here is my visual query:
SOLUTION:
Based on answer, I switched to razor because I am using a custom query. Here is my simple template code now:
#* this will show an "add" button if the current user is an editor *#
#Edit.Toolbar(actions: "new", contentType: "Image")
#{
// get all images as delived from the standard query
var images = AsDynamic(Data["Default"]);
}
<div class="kr-gallery animation">
<p>Hover or touch image and click brush icon for more details</p>
<div class="isotope_grid isotope_grid2">
<div class="isotope_main animation" data-min-width="230">
#foreach(var img in images)
{
<div class="isotope_item kr-gallery-item sc-element">#img.Toolbar
<div class="photo"><a href="#Link.To(parameters: "details=" + img.EntityId)"> <img alt="#img.Title" src="#img.Image?h=500" />
<span class="fa fa-paint-brush"></span></a>
</div>
</div>
}
</div>
</div>
</div>
The missing + is by design, because editors are used to the + adding an item right after the previous one. This behavior cannot be guaranteed with a query, as the order of things is determined by the query. It is even possible, that adding an item will not show up, if a query-parameter hides that item.
So the design pattern is to provide a separate + button. The easiest way is in razor, I believe the code is something like
#Edit.Toolbar(actions: "new", contentType: "your-content-type-name")
In Tokens it's a bit more messy, and you cannot conditionally check if a user has edit-permissions.
So I recommend you go the edit.toolbar way
You can also find an example of this in the blog app: http://2sxc.org/en/apps/app/dnn-blog-app-for-dnn-dotnetnuke
I could be wrong but did you recently experiment with the visual query designer? Because this could be the cause.
The most common reason is when you use a pipeline (visual query) to deliver data to a template, which is not assigned to this instance. Reason is that "add" in a instance-list of items add it to a specific position (like right after the first one). This isn't the same when you use data like a data base - as there is no sorting in that scenario. So if this is the cause, I'll help you more.

Problems getting Iron Router to work on my project

I'm trying to create a quiz app on Meteor and have had trouble setting up Iron Router forever. I'll try to give a visual:
This is the front page:
Image 1
When a user clicks on the button shown above, I want the first question to show up, whose contents are filled from MongoDB.
Image 2
This is what my router looks like ("question" is the name of the question template, as seen in image 2):
Router.route("/quiz/:_id", {
name: "question",
data: function(){
return Quiz.findOne(this.params._id);}
});
Now, in order for me to get from image 1 to image 2, I have to use a mongo object _id in the html file.
<template name="main">
<div class="jumbotron">
<h2>
Welcome to Simple Meteor Quiz app!
</h2>
<p>
To try it out, simply click "start" below!
</p>
<p>
<a class="btn btn-primary btn-large" href="/quiz/cieLkdzvGZPwrnZYE">Start</a>
</p>
</div>
</template>
When I click "Next Question" on image 2 to go onto the 2nd question, it doesn't work. I don't know how to make this process dynamic.
The way it looks to me right now is that I physically have to create a new route for every single question, which would look really ugly really quickly.
Any way to help implement Iron Router in this scenario? I read Discover Meteor and thought I fully understood how Iron Router works, but the more I try to fix this, the more I get confused.
Edit:
To solve my dilemma, I simple created a helper function which I could place behind the /quiz/ in the main template to lead me to the quiz question, based on a suggestion by Michel Floyd.
So the helper ends up looking like this:
Template.main.helpers({
nextQuestion: function(){
queue = Quiz.find().fetch();
return queue[0]._id;
}
});
Then attached to the URL like this:
<a class="btn btn-primary btn-large" href="/quiz/{{nextQuestion}}">Start</a>
Basically just spit out the first _id of first item in the array by making the collection an array via find().fetch(). Will probably randomize the _id at a later time.
You need a way for each template to know what the next question is. For example you can add a nextQuestionId key to your Quiz object. Then your template can be:
<template name="main">
<div class="jumbotron">
<h2>
Welcome to Simple Meteor Quiz app!
</h2>
<p>
To try it out, simply click "start" below!
</p>
<p>
<a class="btn btn-primary btn-large" href="/quiz/{{nextQuestionId}}">
Start
</a>
</p>
</div>
</template>

Simple search functionality in Meteor using form input

So I'm trying to implement a very simple search functionality in my Meteor app. It's a very simple medical dictionary app, so I have only one collection which contains all the terms along with their respective definitions, pronunciations, etc. My goal is for a user to input their search query using a form input, and display the relevant search results (after hitting submit, after keyup or keydown events, doesn't really matter for now; this is just a prototype). Here's what I have so far.
Search Bar (part of a template called header.html)
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" id="search" name="search" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Event Handler (called header.js)
Template.header.events({
'submit form': function(e) {
e.preventDefault();
var query = $(e.target).find('[name=search]').val();
// Log the query for testing
console.log(query);
//Log the object that is returned for testing
console.log(Dictionary.find({english: query}).fetch());
var result = Dictionary.find({english: query}).fetch();
}
});
List of Results (This is the part that doesn't work, in a template called itemsList.html)
<template name="itemsList">
<div class="items">
{{#each dictionary}}
<ul>
<!-- itemPage just refers to the page individual items -->
<li>{{english}}</li>
</ul>
{{/each}}
</div>
</template>
In this case {{english}} refers to the piece of data in the collection I would like to search for (the english word in the collection Dictionary).
So, now that I've got all that out of the way, my question is: what do I do from here? In header.js, the result from console.log(Dictionary.find({english: query}).fetch()); is/are the object(s) I'm looking for, so basically, what must I do to send that object/those objects to my itemsList.html template so I can iterate over it with a cursor?
I think I've been working at this too long, because I'm sure the solution is something really simple. Any help is appreciated, and if my approach is all wrong (I've seen a lot of people using sessions when searching for things), please let me know what I should do better. Also, I'm using iron-router and have autopublish turned off, so if changes need to made to either of those things please let me know that as well. Thanks!
Just save your search string in a session and subscribe using the session as an argument if it exists, that way you can dynamically subscribe to the data that you need to populate your template

Setting javascript function for all <a>

I have a lightbox script that Im using:
<a class="lb-image-link" href="images/image-1.jpg" data-lightbox="lb-set">
<img class="lb-image" src="images/thumb-image-1.jpg" width="150" height="150"/></a>
But for my clients sake to use CMS software I need to make the data-lightbox="lb-set" function work for all <a> tag on the page or to make it work as a class somehow if possible.
The page is HTML, how can I make this work? Im guessing I need to create javascript for this. Any help is greatly appreciated, thank you
One way would be to learn some jquery, if you are not already familiar with it
http://jquery.com/
and then you can easily attach a function to all click events on every a tag on the page with something like:
$("a").click(function(event) {
//your lighbox code goes here ...
});
EDIT
With the lightbox2 script you are using it doesn't look like there is a way to assign it to a group of links on your page based on class or tag type, so the above function is not relevant. At a quick glance, if you stick with that script the only way to have it work on multiple links on the same page is to include a unique data-lightbox="" attribute in each link, eg.
image #1
image #2
image #3
There are other lightbox scripts that allow you to set the lightbox based on tag type or class, for instance http://fancyapps.com/fancybox/ allows you to do the following:
<a class="fancybox" rel="group" href="big_image_1.jpg">image #1</a>
<a class="fancybox" rel="group" href="big_image_2.jpg">image #2</a>
and then use the script
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
the above essentially attaches the fancybox lightbox to anything that has a class="fancybox"
Not sure if that helps - there may not be much difference for you having to add a unique data-lightbox="image-3" to each link as apposed to adding a standard class to them :-)
Glen

Tabs in aspx page

I need to put two tabs on my aspx page (c#). Is there already done tabs control for aspx ?
Ajax has tabs you can use:
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/tabs/tabs.aspx
jQuery also has tabs you could use, which in my opinion is the better choice:
http://jqueryui.com/demos/tabs/
In strictly ASP.NET you can use multiview, and make tabs, here is a tutorial for that:
http://www.codeproject.com/KB/custom-controls/TabControl.aspx
Bootstrap is commonly used with aspx so I used it on my webpage for tabs.
This is a tutorial page.
Please find below an example for two tabs:
<div id="Tabs" role="tabpanel">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="active">Settings tab</li>
<li>Information tab</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="settings">
</div>
<div role="tabpanel" class="tab-pane" id="info">
</div>
</div>
</div>
You need to have bootstrap.css and bootstrap.js references in page head section.
[Reposting my answer because 3 people felt the need to trash my first attempt. One person made it worse by making the URL opaque, so that you can't see it's w3schools.com, a known good tutorial site. Another criticized it for not providing context, like why its approach might be desirable. And a moderator deleted it summarily for none of the reasons I can see posts are supposed to be deleted. Go ahead and remove this commentary, but don't sandbag me and destroy my effort to help others from an ivory tower.]
If you want to send down all the tabs' contents and just have a locally (and instantly) swapped experience, I recommend https://www.w3schools.com/howto/howto_js_tabs.asp. You put your content in divs, and use just a tiny bit of JavaScript to swap out which one is visible. No extra/3rd-party component or framework, just simple HTML/JS that just works.
Super simplistic subset from the above: Content in divs like <div id="tab1">content</div>, then show one with document.getElementById("tab1").style.display = "block"; and hide the others with none instead of block.
There is no standard ASP.NET component, but you can use Microsoft's ASP.NET AjaxControlToolkit library.
Here is the component
You can create simple menus using CSS. This site shows you step by step how to create one, which you can then customise as required.
http://www.secondpicture.com/tutorials/web_design/css_ul_li_horizontal_css_menu.html

Resources