Last active
January 12, 2016 09:58
-
-
Save xMlex/55df731c3ed25ed2e0be to your computer and use it in GitHub Desktop.
Model to model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
appTrainers.factory('ModelPhone', ['$http', function ($http) { | |
function ModelPhone(modelData) { | |
if (modelData) { | |
this.setData(modelData); | |
} | |
} | |
ModelPhone.prototype = { | |
ID_PHONE: '', | |
FID_CONTACT: '', | |
FID_BLOCK: '', | |
FID_LAST_CALL: '', | |
CALL_ATTEMPTS: '', | |
CALL_AMOUNT: '', | |
PHONE_NUMBER: '', | |
PHONE_TYPE: '', | |
onDelete: function(id){console.log("undefined ondel phone");}, | |
setData: function (modelData) { | |
angular.extend(this, modelData); | |
}, | |
update: function () { | |
$http.post('trainers.php?do=phoneSave&id=' + this.ID_PHONE, this); | |
}, | |
delete: function () { | |
var $this = this; | |
$http.post('trainers.php?do=phoneDelete&id=' + this.ID_PHONE, this).success(function (data) { | |
if (data.status) { | |
$this.onDelete($this); | |
} | |
}); | |
} | |
}; | |
return ModelPhone; | |
}]); | |
appTrainers.factory('ModelContact', ['$http', 'ModelPhone', function ($http, ModelPhone) { | |
function ModelContact(modelData) { | |
if (modelData) { | |
this.setData(modelData); | |
} | |
} | |
ModelContact.prototype = { | |
ID_CONTACT: '', | |
FID_REGISTRY: '', | |
FID_BLOCK: '', | |
OPERATOR_LOGIN: '', | |
NEXT_CALL_TIME: '', | |
CALL_AMOUNT: '', | |
PRIORITY: '', | |
FID_LAST_CALL: '', | |
EXTERNAL_ID: '', | |
LAST_NAME: '', | |
PATRONYMIC_NAME: '', | |
TIMEZONE: '', | |
FIRST_NAME: '', | |
CITY: '', | |
DECLINE_REASON: '', | |
COMMENTS: '', | |
DOZVON: '', | |
cityModel: '', | |
phones: [], | |
setCityModel: function (modelData, cityList) { | |
var $this = this; | |
if (modelData) | |
this.CITY = modelData.id; | |
else if (cityList) | |
$.each(cityList, function (k, v) { | |
if (v.id == $this.CITY) | |
$this.cityModel = v; | |
}); | |
}, | |
setData: function (modelData) { | |
angular.extend(this, modelData); | |
}, | |
load: function (id) { | |
var scope = this; | |
$http.get('ourserver/books/' + id).success(function (modelData) { | |
scope.setData(modelData); | |
}); | |
}, | |
delete: function () { | |
$http.delete('ourserver/books/' + this.id); | |
}, | |
onDeletePhone: function (id) { | |
var $this = this; | |
console.dir(this); | |
if($this.phones) | |
$.each($this.phones, function (k, v) { | |
if (v.ID_PHONE == id.ID_PHONE) { | |
var index = $this.phones.indexOf(v); | |
$this.phones.splice(index, 1); | |
} | |
}); | |
}, | |
getPhones: function () { | |
var scope = this; | |
$http.get('trainers.php?do=phoneList&id=' + this.ID_CONTACT).success(function (modelData) { | |
scope.phones = []; | |
$.each(modelData, function (k, v) { | |
var el = new ModelPhone(v); | |
el.onDelete = scope.onDeletePhone; | |
scope.phones.push(el); | |
}); | |
}); | |
}, | |
update: function () { | |
if (this.cityModel != '') | |
this.CITY = this.cityModel.id; | |
$http.post('trainers.php?do=save&id=' + this.ID_CONTACT, this); | |
} | |
}; | |
return ModelContact; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment