Angularjs, search form, factory: Button doesn't work - forms

I have a typical form-with-query-editor and search button im my Angularjs-app. However, clicking the search-button doesn't work. If I initially open the form with the table, the service works. But not when clicking the search button. The code is as follows:
HTML vlist.html:
<input ng-model="nachname" type="text" id="nachname" name="nachname" class="search-query" />
<button ng-click="search()" type="submit" class="btn btn-primary">Search</button>
...
<tr ng-repeat="pers in personen">
<td>{{pers.nachname}}</td>
</tr>
...
Controller:
var app = angular.module( 'cdemoApp', [ 'cdemo.services' ] );
app.config( ['$routeProvider', function( $routeProvider ) {
$routeProvider.
when( '/vlist', {
controller: 'VListCtrl',
resolve: {
personlistdto: function( VListLoader ) {
return VListLoader();
}
},
templateUrl : 'app/view/vlist.html'
} ).otherwise( { redirectTo: '/' } );
} ] );
app.controller( 'VListCtrl', [ '$scope', 'personlistdto',
function( $scope, personlistdto ) {
$scope.search = function(){
$scope.personen = personlistdto.aaData;
$scope.iTotalRecords = personlistdto.iTotalRecords;
};
$scope.search();
}]);
Service:
var services = angular.module( 'cdemo.services', [ 'ngResource' ] );
services.factory( 'Vers', [ '$resource',
function find( $resource ) {
return $resource( '/cdemo/rest/vers/ajs/:id',
{ id: '#id', isArray: false }
);
} ] );
services.factory( 'VListLoader', [ 'Vers', '$q',
function( Vers, $q ) {
console.log('Vloader1');
var find = function find() {
var delay = $q.defer();
Vers.get( function( personlistdto ) {
delay.resolve( personlistdto );
}, function() {
delay.reject( 'Nix fetch' );
} );
return delay.promise;
};
return find;
} ] );
Any idea how I can get the button searching (including fields/parameters) - because nothing happens when clicking the button? Thanks for an answer.

I got it working by changing the controller like this:
app.controller( 'VListCtrl', [ '$scope', 'personlistdto', 'VListLoader',
function( $scope, personlistdto, VListLoader ) {
$scope.personlistdto = personlistdto;
$scope.searchFactory = VListLoader;
$scope.search = function( ){
$scope.personlistdto = $scope.searchFactory();
};
}
] );
This way the Factory VListLoader is called.

Related

My dateFormat is not working for Flatpickr in Mobile

I am using Datepicker of Flatpickr and I have set the date format and it is working fine in desktop but in mobile it is not working properly.
This is my code that I added in functions.php:
add_action( 'wp_footer', function() {
?>
<script type="text/javascript">
jQuery( window ).load( function( $ ){
var limitFlatPicker;
var afterTwoDays;
var afterEightDays;
limitFlatPicker = limitFlatPicker || {};
limitFlatPicker = {
defaultSettings: {
selector: '.flatpickr-input',
minDate: true,
maxDate: true,
},
settings: {},
init: function( options ) {
this.settings = jQuery.extend( this.defaultSettings, options );
if ( this.settings.minDate || this.settings.maxDate ) {
this.waitForFlatpicker( this.callback );
}
},
waitForFlatpicker: function( callback ) {
if ( typeof window.flatpickr !== 'function' ) {
setTimeout( function() { limitFlatPicker.waitForFlatpicker( callback ) }, 100 );
}
callback();
},
modifyPicker: function( picker, settings ) {
flatpickr( picker ).set( settings );
},
callback: function() {
var self;
self = limitFlatPicker;
jQuery( self.settings.selector ).each( function() {
var picker;
picker = jQuery( this )[0],
pickerSettings = {};
if ( self.settings.minDate ) {
pickerSettings.minDate = self.settings.minDate;
}
if ( self.settings.maxDate ) {
pickerSettings['maxDate'] = self.settings.maxDate;
}
if ( self.settings.dateFormat ) {
pickerSettings.dateFormat = self.settings.dateFormat;
}
self.modifyPicker( picker, pickerSettings );
} );
}
}
limitFlatPicker.init({
minDate: new Date(),
selector: '#form-field-form_field_date',
dateFormat: 'm-d-Y',
});
} );
</script>
<?php
},11);
The dateFormat: 'm-d-Y' is working fine in desktop and in mobile, it is not showing this dateformat.
Any help is much appreciated.

populate select with datajson using React js

I'm trying to populate a select using React js, I'm using the example given on the react js docs(https://facebook.github.io/react/tips/initial-ajax.html) , which uses jquery to manage the ajax calling, I'm not able to make it work, so far i have this:
here the codepen : http://codepen.io/parlop/pen/jrXOWB
//json file called from source : [{"companycase_id":"CTSPROD","name":"CTS-Production"},{"companyc ase_id":"CTSTESTING","name":"CTS-Testing"}]
//using jquery to make a ajax call
var App = React.createClass({
getInitialState: function() {
return {
opts:[]
};
},
componentDidMount: function() {
var source="https://api.myjson.com/bins/3dbn8";
this.serverRequest = $.get(source, function (result) {
var arrTen = result[''];
for (var k = 0; k < ten.length; k++) {
arrTen.push(<option key={opts[k]} value={ten[k].companycase_id}> {ten[k].name} </option>);
}
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function() {
return (
<div>
<select id='select1'>
{this.state.opts}
</select>
</div>
);
}
});
ReactDOM.render(
<App />,
document.getElementById('root')
);
html
<div id="root"></div>
any idea how to make it works, thanks.
You need to call setState to actually update your view. Here's a workable version.
//json file called from source : [{"companycase_id":"CTSPROD","name":"CTS-Production"},{"companyc ase_id":"CTSTESTING","name":"CTS-Testing"}]
//using jquery to make a ajax call
var App = React.createClass({
getInitialState: function() {
return {
opts:[]
};
},
componentDidMount: function() {
var source="https://api.myjson.com/bins/3dbn8";
this.serverRequest = $.get(source, function (result) {
var arrTen = [];
for (var k = 0; k < result.length; k++) {
arrTen.push(<option key={result[k].companycase_id} value={result[k].companycase_id}> {result[k].name} </option>);
}
this.setState({
opts: arrTen
});
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function() {
return (
<div>
<select id='select1'>
{this.state.opts}
</select>
</div>
);
}
});
ReactDOM.render(
<App />,
document.getElementById('root')
);

How to test slide-box in protractor

I have a slide-box described in one of my protractor tests; I can find the box and can get properties (i.e. 'how many') but how do I cycle through the boxes so I can test verify the display, e.g.
profilepage.slides.next()
expect(profilepage.slide.slideTitle = 'Credentials'
profilepage.slides.next()
expect(profilepage.slide.slideTitle = "Info"
etc.
Controller:
.controller('ProfileCtrl', function ($scope, ProfileService) {
$scope.data = {
numViewableSlides: 0,
slideIndex: 0,
initialInstruction: true,
secondInstruction: false, slides: [
{
'template': 'templates/slidebox/credentials.html',
'viewable': true
},
{
'template': 'templates/slidebox/contactinfo.html',
'viewable': true
},
{
'template': 'templates/slidebox/employeeinfo.html',
'viewable': true
},
{
'template': 'templates/slidebox/assignmentinfo.html',
'viewable': true
}
]
}
. . .
Template:
<ion-slide-box on-slide-changed="slideChanged(index)" show-pager="true">
<ion-slide ng-repeat="slide in data.slides | filter:{viewable : true}">
<div ng-include src="slide.template"></div>
</ion-slide>
</ion-slide-box>
Page Object:
profilepage.prototype = Object.create({}, {
backButton: {
get: function () {
return element(by.css('ion-ios7-arrow-back'));
}
},
slides: {
get: function () {
return element.all(by.repeater('slide in data.slides'));
}
},
slideTitle: {
get: function (id) {
element.all(by.repeater('slide in data.slides')).then(function (slidelist) {
var titleElement = slidelist[id].element(by.css('#slideName'));
return titleElement.getText();
});
}
},
. . .
Spec:
describe('Profile', function () {
var ppage = new profilepage();
beforeEach(function () {
browser.ignoreSynchronization = false;
});
it('should have correct lastname and have four slides on profile page', function () {
expect(browser.getCurrentUrl()).toEqual('http://localhost:8100/#/profile');
expect(ppage.lastname).toBe('Smith,');
expect(ppage.slides.count()).toEqual(4);
browser.sleep(1000);
});
it('should slide all the pages', function(){
expect(browser.getCurrentUrl()).toEqual('http://localhost:8100/#/profile');
// SLIDE EACH PAGE ABOUT HERE <------------
browser.sleep(1000);
})
The idea is to use ionic's $ionicSlideBoxDelegate from within the spec file. For that we'll need to make it accessible globally:
var addProtractorSlideBox, nextSlide;
addProtractorSlideBox = function() {
return browser.addMockModule("services", function() {
return angular.module("services").run(function($ionicSlideBoxDelegate) {
return window._$ionicSlideBoxDelegate = $ionicSlideBoxDelegate;
});
});
};
nextSlide = function() {
return browser.executeScript('_$ionicSlideBoxDelegate.next()');
};
...
beforeEach(function() {
...
addProtractorSlideBox();
...
});
it('...', function() {
...
nextSlide();
...
})
This pattern is very useful for other ionic/angular services.

bootstrap typeahead url/redirect

$(function(){
var orthoObjs = {};
var orthoNames = [];
var throttledRequest = _.debounce(function(query, process){
$.ajax({
url: 'json/ortho4.json'
,cache: false
,success: function(data){
orthoObjs = {};
orthoNames = [];
_.each( data, function(item, ix, list){
orthoNames.push( item.searchPhr );
orthoObjs[ item.searchPhr ] = item;
});
process( orthoNames );
}
});
}, 300);
$(".typeahead").typeahead({
source: function ( query, process ) {
throttledRequest( query, process );
}
,updater: function (item) {
var url = "orthoObjs[item.searchUrl]";
window.location = url;
Whats the best way to get the redirect to work? I have seen similar questions, but can't get this to work. Documentation on typeahead isn't great. I am using underscore.js for the each function. Just want a simple search query that redirects when the user selects.
I actually got this to work. I got a little help... but here it is. There is the JSON file..
[
{ "id":1, "searchUrl":"invisalign.html", "name":"invisalign" }
,{ "id":2, "searchUrl":"invisalign.html", "name":"invisalign teen" }
,{ "id":3, "searchUrl":"clearbraces.html", "name":"clear braces" }
]
And the HTML code....
Lots of good stuff here.. http://fusiongrokker.com/post/heavily-customizing-a-bootstrap-typeahead
And the search code..
<form method="post" id="myForm" class="navbar-search pull-left">
<input
type="text"
class="search-query typeahead"
placeholder="Search Our Website"
autocomplete="off"
data-provide="typeahead"
/>
<i class="fa-icon-search icon-black"></i>
</form> </li>
$(function(){
var bondObjs = {};
var bondNames = [];
$(".typeahead").typeahead({
source: function ( query, process ) {
//get the data to populate the typeahead (plus an id value)
$.ajax({
url: '/json/bonds.json'
,cache: false
,success: function(data){
bondObjs = {};
bondNames = [];
_.each( data, function(item, ix, list){
bondNames.push( item.name );
bondObjs[ item.name ] = item.searchUrl;
});
process( bondNames );
}
});
}
, updater: function ( selectedName ) {
window.location.href =bondObjs[ selectedName ];
}
});
});
</script>

Geolocation after the device is ready

I have a iphone app built with jquery mobile which is wrapped in phonegap. I am trying to get geolocation after the device is ready to get rid of the nasty alert /var/mobile/Applications/157EB70D-4AA7-826E-690F0CBE0F/appname.app/www/index.html.
I have set some alerts to see if the device is ready and it keeps saying isDeviceReady is:false which is meaning the device is not ready
he is the code
$(function() {
var isWatching = false;
var isAndroid = ( navigator.userAgent.indexOf('Android') != -1 ) ? true : false;
var isDeviceReady = false;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
isDeviceReady = true;
}
function getClientPosition(id, successCallback, errorCallback) {
if ( isDeviceReady ) {
var hasFoundMarker = false;
if ( hasClientPosition() ) {
$(id).gmap('findMarker', 'tag', 'client', function(found, marker) {
if (found) {
hasFoundMarker = true;
marker.setPosition(getClientLatLng());
$(id).gmap('getMap').setCenter(marker.getPosition());
if ( $.isFunction(successCallback) ) {
successCallback.call(this, getClientLatLng());
}
}
});
if ( !hasFoundMarker ) {
addClientMarker(id, getClientLatLng());
if ( $.isFunction(successCallback) ) {
successCallback.call(this, getClientLatLng());
}
}
}
if ( !isWatching[id] ) {
if ( navigator.geolocation ) {
isWatching[id] = true;
if ( isAndroid ) {
watch[id] = setInterval(function() {
navigator.geolocation.getCurrentPosition (
function( position ) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$(id).gmap('findMarker', 'tag', 'client', function(found, marker) {
if (found) {
hasFoundMarker = true;
marker.setPosition(latlng);
$(id).gmap('getMap').setCenter(latlng);
}
});
if ( !hasFoundMarker ) {
addClientMarker(id, latlng);
}
setData(CLIENT_HAS_POSITION, true);
setData(CLIENT_LATITUDE, latlng.lat());
setData(CLIENT_LONGITUDE, latlng.lng());
if ( $.isFunction(successCallback) ) {
successCallback.call(this, latlng);
}
},
function( error ) {
if ( $.isFunction(errorCallback) ) {
errorCallback.call(this, error);
}
},
opts.geolocationOptions
);
}, 5000);
} else {
watch[id] = navigator.geolocation.watchPosition (
function( position ) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$(id).gmap('findMarker', 'tag', 'client', function(found, marker) {
if (found) {
hasFoundMarker = true;
marker.setPosition(latlng);
$(id).gmap('getMap').setCenter(latlng);
}
});
if ( !hasFoundMarker ) {
addClientMarker(id, latlng);
}
setData(CLIENT_HAS_POSITION, true);
setData(CLIENT_LATITUDE, latlng.lat());
setData(CLIENT_LONGITUDE, latlng.lng());
if ( $.isFunction(successCallback) ) {
successCallback.call(this, latlng);
}
},
function( error ) {
if ( $.isFunction(errorCallback) ) {
errorCallback.call(this, error);
}
},
opts.geolocationOptions
);
}
}
}
} else {
var timer = setTimeout(function() {
getClientPosition(id, successCallback, errorCallback);
alert('Trying to get client position. This message will pop up again in 15 sec, unless device is ready. IsDeviceReady is: '+isDeviceReady);
}, 15000);
}
}
Anyone have any ideas where i am going wrong?