how do get a controller of a view
var c = Alloy.createController('win', activeTab);
c = c.getView();
Wins.push(c);
in controller win i have function
exports.fun = function() {
};
after getting the win from controller which is the view how do i call this function from a view i need controller to call the function
for ( i = 0; i < Wins.length; i++) {
Wins[i].fun();
}
Wins[i] is a View how do i get a controller of this view so that i can call the function fun()
dont push the window, push the controller
// this is a bad name for a controller...
var controller = Alloy.createController('win', activeTab);
var view = controller.getView();
// save the controller to a list of global controllers
Alloy.Globals.Controllers = Alloy.Globals.Controllers || {};
Alloy.Globals.Controllers['aController'] = controller;
// loop through all controller and execute func if it exists
for ( var i in Alloy.Globals.Controllers) {
Alloy.Globals.Controllers[i].fun && Alloy.Globals.Controllers[i].fun();
}
Related
I've problem with AngularJS. Ng-repeat dosn't want to refresh loop when I add new item to JSON from another instance of controller
In first controller I set JSON
(function(){
'use strict';
angular.module('mainApp').controller('ListController', function($scope, FavoriteListService) {
$scope.addToFavorites = function(speaker){
FavoriteListService.setFavorites(speaker);
};
})();
In secound controller I have to display ng-repeat
(function(){
'use strict';
angular.module('mainApp').controller('ShowController', function($scope, FavoriteListService) {
$scope.favoritesList = FavoriteListService.getFavorites();
})();
Factory
(function () {
'use strict';
angular.module('mainApp').factory('FavoriteListService', function () {
var obj = {};
obj.getFavorites = function () {
var favorites = localStorage.getItem('speaker-favorites');
if (favorites == null) {
favorites = {};
} else {
favorites = JSON.parse(favorites);
}
return favorites;
};
obj.setFavorites = function (speaker) {
var favorites = obj.getFavorites();
favorites[speaker.uid] = {firstname: speaker.firstname, name: speaker.name};
localStorage.setItem('speaker-favorites', JSON.stringify(favorites));
};
return obj;
});
})();
Template:
<ul>
<li ng-repeat="(key, fav) in favoritesList">
{{fav.firstname}} {{fav.name}}
</li>
</ul>
Everything is fine, when set & display is in one controller.
If I want to use 2 controllers (or 2 instance of 1 controller) ng-repeat show correctly all items after load page, but when I add new item it doesn't refresh loop and doesn't show new item.
Is any method to fix it?
You either need to change repeater to (and assign that FavoriteListService to $scope variable):
ng-repeat="(key, fav) in FavoriteListService.getFavorites()"
or $watch that favorite list in your controller like that:
$scope.$watch(
function() { return FavoriteListService.getFavorites(); },
function(newValue, oldValue) {
if ( newValue !== oldValue ) {
$scope.favoritesList = newValue;
}
},
true
);
Because when you assign your service method return to scope method it's not being working like a reference.
Create an empty object, and use angular.copy to update the object:
app.factory('FavoriteListService', function () {
var obj = {};
//create empty object
var favorites = {};
obj.getFavorites = function () {
let update = localStorage.getItem('speaker-favorites');
if (update) {
//Use angular copy
angular.copy(update, favorites);
};
return favorites;
};
obj.setFavorites = function (speaker) {
favorites[speaker.uid] = {firstname: speaker.firstname, name: speaker.name};
localStorage.setItem('speaker-favorites', JSON.stringify(favorites));
};
return obj;
});
By using angular.copy, all of the controllers that use the getFavorites function get the same object reference and all of them see the same changes to its contents.
For more information, see AngularJS angular.copy API Reference
I'm build an application with appcelerator-studio.
The application create programmatically a table with this code:
controller.js
var rowData = [];
var header = Alloy.createController('result_row_header',{
examination:Titanium.Locale.getString(lang+"examination"),
start_date:Titanium.Locale.getString(lang+"start_date"),
end_date:Titanium.Locale.getString(lang+"end_date")
});
var viewHeader = header.getView();
//DISEGNO LA TESTATA
rowData.push(viewHeader);
$.table.setData(rowData);
function set_fields(lang) {
header.changeLanguage(lang);
}
result_row_header.js
var args = arguments[0] || {};
$.examination.text = args.examination;
$.start_date.text = args.start_date;
$.end_date.text = args.end_date;
function changeLanguage(lang){
$.examination.text=Titanium.Locale.getString(lang+"examination");
}
Now I want to call the changeLanguage method from controller.js but if I try to execute the method set_fileds (from controller.js) I have an error.
Just a little change in your code:
result_row_header.js
var args = arguments[0] || {};
$.examination.text = args.examination;
$.start_date.text = args.start_date;
$.end_date.text = args.end_date;
// this is the code to access function of a controller from any other
$.changeLanguage = function (lang){
$.examination.text=Titanium.Locale.getString(lang+"examination");
};
Rest of your code remains same.
Lets says that this is your view.js file:
$.yourMethod = function() {
return 'first';
};
$.view.yourMethod = function() {
return 'second';
};
And this is your view.xml:
<Alloy>
<View id="view">
<Label text="test view"/>
</View>
</Alloy>
You call it like this:
var view = Alloy.createController("view",{});
view.yourMethod(); //will return first
win.add(view.getView());
But if you get the view at the creation method, you call it like this:
var view = Alloy.createController("view",{}).getView();
view.yourMethod(); //will return second
win.add(view);
Titanium Alloy Controller Constructor doesn't exposer functions
When I load my json data from the server I need to have additional view only properties on json data. But thats not possible.
Then I thought about creating angularjs models from factories like:
'use strict';
angular.module('TGB').factory('TestViewModel', function () {
function TestViewModel(test) {
this.id = test.id;
this.schoolclassCode = test.schoolclassCode;
this.testType = test.type;
this.creationDate = test.date;
this.number = test.number;
this.isExpanded = false;
}
return (TestViewModel);
});
Where do you create these viewmodels in angular?
At the moment I have this method in my Controller , call it there and assign the result to $scope.testViewModels = toTestListViewModel(tests)
function toTestListViewModel(tests)
{
var testListViewModel = [];
for (var i = 0; i < tests.length; i++) {
var testViewModel = new TestViewModel(tests[i]);
testListViewModel.push(testViewModel);
}
return testListViewModel;
}
Controller is a proper place to create your 'view model' that can be accessed using $scope.
.controller('sampleCtrl', function ($scope, testViewModel) {
$scope.vM = testViewModel;
}
everyone!
I'm creating app with authorization.
To store user data during session I use such service:
app.service('Session', function ($rootScope, $cookies) {
var Session = $rootScope.$new();
Session.createSession = function (accessTo, level, user_data) {
this.level = level;
this.accessTo = accessTo;
if(user_data) {
$cookies.isAuth = true;
} else {
$cookies.isAuth = false;
}
this.user = {
email : user_data.email,
fullname : user_data.fullname,
id : user_data.id,
level : user_data.level
}
if(accessTo && accessTo.length == 1 && !$cookies.account) {
this.account = accessTo[0].id;
$cookies.account = accessTo[0].id;
} else {
this.account = $cookies.account;
}
};
Session.destroy = function () {
this.level = null;
this.account = null;
this.user = null;
this.isAuth = false;
$cookies.account = null;
};
return Session;
});
In controller use:
Session.createSession(some, params, here);
Afteer that it put some data to rootscope, and i can show it in console, but when i try to see for example Session.user/.level etc. it doesn't work. What is wrong?
You should be doing this:
app.service('Session', function() {
// A service is a singleton, as in it a persistent
// object you can inject in controllers
this.foo = 'bar'; // anything on "this" is publicly available
this.createSession = function() { ... };
// you can also expose functions that you can call from the controllers
});
app.controller('Ctrl1', function($scope, Session) {
// You can inject "Session" here and use stuff you
// added on "this" inside of it
$scope.foo = Session.foo;
// you can put stuff on the controller's $scope
// and you can bind to it in your HTML by using <span>{{foo}}</span>
Session.foo = 'baz';
// you can change stuff in the service and it will be persisted
});
So now if you navigate to a certain "Ctrl2" that injects "Session" too, Session.foo will be "baz", not the initial "bar" value.
I have three js controller file and 1 lib
app/lib/Client
function Client(id,name,blc){
this.id=id;
this.name=name;
this.blc=blc;
};
Client.prototype.getName = function(){
return this.id+' '+this.name+' '+this.blc;
};
Client.prototype.withdraw = function(amount){
if(amount<0) return -1;
if(this.blc<amount) return -1;
return this.blc-=amount;
};
Client.prototype.deposite = function(amount){
if(amount<0) return -1;
return this.blc+=amount;
};
module.exports = Client;
app/controller/addClient //this is where I want to add to global array
var args = arguments[0] || {};
var Client = require('Client');
function doClick(e) {
var user_id = $.id.getValue();
var user_name = $.name.getValue();
var user_blc = $.Balance.getValue();
if(user_id.length<=0 && user_name.length<=0 && user_blc.length<=0){
alert('you entred an invalid information');
}
else{
var c = new Client(user_id,user_name,user_blc);
alert(c.getName()+' is add successfly');
//note!!!!!
//add a global array to save the data
$.id.setValue("");
$.name.setValue("");
$.Balance.setValue("");
}
}
$.addClient.open();
app/controller/allClient
//this is where i want to use the data in Global app to make a listView
If you really want a "global array" use Alloy.Globals
If you want a collection of models, use Backbone