Why angular factory not working as expected - javascript

Below is simple angular factory which almost works. I want to reset my model and I tried to add a method called 'resetModel' but it doesn't work as It is not resetting the model properties. Could someone please explain why?
app.factory('programLocationModel', [ "$rootScope", function ($rootScope)
{
var ProgramLocationModel = function()
{
this.name = "All Programmes";
this.description = "";
this.category = "";
this.series = {};
this.channel = {};
this.duration = "";
this.airTime = "";
this.seriesName = "";
this.url = "../assets/images/nhkw_thumbnail.jpg"; //Default client logo
}
ProgramLocationModel.prototype.update = function( data )
{
this.name = data.name;
this.description = data.description;
this.category = data.category;
this.series = data.series;
this.seriesName = data.seriesName;
this.channel = data.channel;
this.duration = data.duration;
this.airTime = data.airTime;
this.url = $rootScope.resturl + '/graph/' + data.id + '/thumbnail?access_token=' + $rootScope.token;
}
ProgramLocationModel.prototype.resetModel = function () {
ProgramLocationModel();
}
return new ProgramLocationModel();
} ] );

Your resetModel function is only calling the constructor and not doing anything to the actual instance the method is called on. Your resetModel function is supposed to modify the properties of this, just like you already do in the constructor and in your update method.
Here is a simple way to do it:
app.factory('programLocationModel', [ "$rootScope", function ($rootScope)
{
var ProgramLocationModel = function()
{
this.resetModel();
}
ProgramLocationModel.prototype.update = function( data )
{
this.name = data.name;
this.description = data.description;
this.category = data.category;
this.series = data.series;
this.seriesName = _seriesName;
this.channel = data.channel;
this.duration = data.duration;
this.airTime = data.airTime;
this.url = $rootScope.resturl + '/graph/' + data.id + '/thumbnail?access_token=' + $rootScope.token;
}
ProgramLocationModel.prototype.resetModel = function () {
this.name = "All Programmes";
this.description = "";
this.category = "";
this.series = {};
this.channel = {};
this.duration = "";
this.airTime = "";
this.seriesName = "";
this.url = "../assets/images/nhkw_thumbnail.jpg"; //Default client logo
}
return new ProgramLocationModel();
} ] );

Related

Set value into object with function (java script)

When I used getFullName, getFirstName and getLastName work ok, but I can't use set functions setFullName, setLastName, setFirstName. My code:
var Person = function(firstAndLast) {
var fn=firstAndLast.split(' ');
var fstr=fn.join(' ');
var frn=fn[0];
var lsn=fn[1];
this.getFullName=function(){return fstr;};
this.getFirstName=function(){return frn;};
this.getLastName=function(){return lsn;};
this.setFirstName=function(a){fn[0]=a;};
this.setLastName=function(b){fn[1]=b;};
this.setFullName=function(c){fn=c.split(' ');};
};
What about this:
var Person = function(firstAndLast) {
var self = this;
this.fn = firstAndLast.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
this.getFullName=function(){return self.fn.join(' ');};
this.getFirstName=function(){return self.frn;};
this.getLastName=function(){return self.lsn;};
this.setFirstName=function(a){self.frn=a; self.fn[0]=a;};
this.setLastName=function(b){self.lsn=b; self.fn[1]=b;};
this.setFullName=function(c){
self.fn = c.split(' ');
self.frn = this.fn[0];
self.lsn = this.fn[1];};
};
See this fiddle
If you have a lot of Person objects, you should consider moving the getter/setter functions to the class prototype:
var Person = function(firstAndLast) {
this.fn = firstAndLast.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
};
Person.prototype.getFullName = function() {
return this.fn.join(' ');
}
Person.prototype.getFirstName = function() {
return this.lsn;
}
Person.prototype.getLastName = function() {
return this.lsn;
}
Person.prototype.setFirstName = function(a) {
this.frn=a;
this.fn[0]=a;
}
Person.prototype.setLastName = function(b) {
this.lsn=b;
this.fn[1]=b;
}
Person.prototype.setFullName = function(c) {
this.fn = c.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
}
See updated fiddle

How to change the properties with factory method in javascript

i have one class i am trying to overwrite it with factory design pattern but not able to do that..
how to call it exactly to change the properties of object
function othername() {
var newobj = new Object();
newobj.fname = "sachin",
newobj.lname = "rawal",
newobj.fullname = function () {
alert(this.fname);
}
return newobj
}
var othername1 = othername ("hi","hello");
Using prototype you can do it.
function othername() {
var newobj = new Object();
newobj.fname = "sachin",
newobj.lname = "rawal",
newobj.fullname = function () {
alert(this.fname);
}
return newobj
}
var othername1 = othername ("hi","hello");
prototype:
othername.prototype.middleName = "middleNameString";
var named = othername();
var md = named.middleName // md == "middleNameString"
function Othername(fname,lname) {
this.fname = fname || "sachin";
this.lname = lname || "rawal";
};
Othername.prototype.fullname = function (){
alert(this.fname + " " + this.lname);
};
var othernameDefault = new Othername (); //sachin rawal
var othername1 = new Othername ("hi","hello"); //hi hello

How to add a row on button click?

in this fiddle there is an add timing button. There is default row having week day dropdown,from time textfield,to time textfield and hospital drop down.Now when I click on the add timing button I want another row having week day dropdown,from time textfield,to time textfield and hospital dropdown.
Can any body please tell me how to do that?
This is my knockout code
var DocSchedule = function (id, day, fromtime, totime, hospital, hospitalId) {
this.id = ko.observable(id);
this.day = ko.observable(day);
this.fromtime = ko.observable(fromtime);
this.totime = ko.observable(totime);
this.hospital = ko.observable(hospital);
this.hospitalId = ko.observable(hospitalId);
};
var Patientp = function () {
this.id = ko.observable(idValue);
this.name = ko.observable(nameValue);
this.degree = ko.observable(degreeValue);
this.gender = ko.observable(genderValue);
//this.consultant= ko.observableArray(consultantArrValue);
this.username = ko.observable(usernameValue);
this.password = ko.observable(passwordValue);
this.email = ko.observable(emailValue);
this.mobile = ko.observable(mobileValue);
this.imgFile = ko.observable(imgFileValue);
this.imgSrc = ko.observable(imgSrcValue);
this.imagePath = ko.observable(imagePathValue);
this.userid = ko.observable(useridValue);
this.department = ko.observable(departmentValue);
//this.consultant= ko.observableArray(consultantArrValue);
//this.consultant= ko.observable(consultantValue);
this.addSlot = function (doctor) {
var docSchedule = new DocSchedule();
iter = iter + 1;
docSchedule.id(iter);
}
}
idValue = 'hi';
useridValue = 'hi';
nameValue = 'hi';
addressValue = 'adress';
genderValue = 'hi';
mobileValue = 'hi';
//these fields are not available
usernameValue = 'hi';
passwordValue = 'hi';
emailValue = 'hi';
imgFileValue = 'imagefileValue';
imgSrcValue = 'ui_resources/img/profile_pic.png';
imagePathValue = 'imagePathValue';
consultantArrValue = null; //'${currentpatient.user.name}';
consultantValue = "d1";
degreeValue = 'hi';
departmentValue = 'hi';
var iter = 0;
var patp = new Patientp();
ko.applyBindings(patp);
You don't have observableArray schedules in Patientp doctor.schedules.push(docSchedule);
throws Uncaught TypeError: Cannot read property 'push' of undefined
Try this:
var iter = 0;
var DocSchedule = function (id, day, fromtime, totime, hospital, hospitalId) {
this.id = ko.observable(id);
this.day = ko.observable(day);
this.fromtime = ko.observable(fromtime);
this.totime = ko.observable(totime);
this.hospital = ko.observable(hospital);
this.hospitalId = ko.observable(hospitalId);
};
var Patientp = function () {
this.id = ko.observable(idValue);
this.name = ko.observable(nameValue);
this.degree = ko.observable(degreeValue);
this.gender = ko.observable(genderValue);
this.username = ko.observable(usernameValue);
this.password = ko.observable(passwordValue);
this.email = ko.observable(emailValue);
this.mobile = ko.observable(mobileValue);
this.imgFile = ko.observable(imgFileValue);
this.imgSrc = ko.observable(imgSrcValue);
this.imagePath = ko.observable(imagePathValue);
this.userid = ko.observable(useridValue);
this.department = ko.observable(departmentValue);
this.schedulers = ko.observableArray([]);
}
idValue = 'hi';
useridValue = 'hi';
nameValue = 'hi';
addressValue = 'adress';
genderValue = 'hi';
mobileValue = 'hi';
usernameValue = 'hi';
passwordValue = 'hi';
emailValue = 'hi';
imgFileValue = 'imagefileValue';
imgSrcValue = 'ui_resources/img/profile_pic.png';
imagePathValue = 'imagePathValue';
consultantArrValue = null;
consultantValue = "d1";
degreeValue = 'hi';
departmentValue = 'hi';
function vm() {
var self = this;
self.person = new Patientp();
self.schedule = new DocSchedule();
self.schedules = ko.observableArray([new DocSchedule(iter)]);
self.addSlot = function () {
console.log('added');
iter++;
var docSchedule = new DocSchedule(iter);
self.schedules.push(docSchedule);
};
self.removeSlot = function () {
console.log('removed');
self.schedules.remove(this);
}
};
var viewModel = new vm();
ko.applyBindings(viewModel, document.getElementById('addDoctorSchedules'));
Here is the Demo

OO Javascript Game, how can i add players the game's player array?

Here is the beginning of a very simple object oriented javascript game. I am having trouble adding players to the Games players[] array. how can this be done?
i've removed all the code not relevant to the question
var BobsGame = function(){
this.Players = [];
}
BobsGame.prototype.addPlayer = function(Player){
//not working
Player.Game = this;
this.Players.push(Player);
Player.setId = function(this.Players.length) {
return this.Players.length;
}
return this;
}
var Player = function(name){
this.Game = null;
this.score = 0;
this.name = name;
this.id = null;
// return alert('Player ' + this.name + ' has been created');
}
var myGame = new BobsGame();
var Player1 = new Player("Tom");
var Player2 = new Player("Bob");
myGame.addPlayer(Player1);
What I think you're trying:
var BobsGame = function () {
this.Players = [];
};
BobsGame.prototype.addPlayer = function (Player) {
this.Players.push(Player);
Player.id = this.Players.length;
return this;
};
var Player = function (name) {
this.score = 0;
this.name = name;
this.id = null;
// return alert('Player ' + this.name + ' has been created');
};
var myGame = new BobsGame();
var Player1 = new Player("Tom");
var Player2 = new Player("Bob");
myGame.addPlayer(Player1);
The error was in the following code:
Player.setId = function(this.Players.length) {
return this.Players.length;
}
Here you're trying creating a function called setId, but you can't define this.Players.length as an argument. So assuming you're trying to set the id of the player to the length of the players array, you can simple assign it like this:
Player.id = this.Players.length;
By the way, I do recommend you don't call the argument of addPlayer "Player" since this will hide the already defined function Player. You might want to rename it to "player".

knockout.js not mapping observable array from json

Can't map json to observable array. I use code from tutorial (http://learn.knockoutjs.com/).
function Movie(data) {
this.name = ko.observable(data.name);
this.description = ko.observable(data.description);
this.duration = ko.observable(data.duration);
this.id = ko.observable(data.id);
this.imdb_id = ko.observable(data.imdb_id);
this.original_name = ko.observable(data.original_name);
this.poster = ko.observable(data.poster);
this.type = ko.observable(data.type);
this.year = ko.observable(data.year);
}
function MovieListViewModel() {
// Data
var self = this;
self.moviesArray = ko.observableArray([]);
self.searchQuery = ko.observable();
self.searchMovies = function () {
$.getJSON("/api/v1/movies/search/", {"query": self.searchQuery }, function(allData) {
var mappedMovies = $.map(allData.movies, function(item) { return new Movie(item) });
console.log(mappedMovies); // in this line output: [Movie, Movie, Movie, Movie, Movie, Movie]
self.moviesArray(mappedMovies);
console.log(self.moviesArray); // in this line output: []
});
};
}
ko.applyBindings(new MovieListViewModel());
I do not understand what is wrong.
P.S. Sorry for my english
This
{"query": self.searchQuery }
Should be
{"query": self.searchQuery() }
This
console.log(self.moviesArray)
Should be
console.log(self.moviesArray())

Categories

Resources