Javascript //步骤2:如果这是用于折叠/扩展联系人。。。 if(evt.target.id.indexOf('italic_toggle')>=0){ var sourceElem=$(evt.target); sourceElem.toggleClass('fa-chevron-circle-down'); sourceElem.toggleClass(“fa-chevron-circle-up”); var targetElem=$('#'+evt.target.id.replace('italic','div'); 目标元素滑动切换(600); } } });
路由、控制器和模型定义如下:Javascript //步骤2:如果这是用于折叠/扩展联系人。。。 if(evt.target.id.indexOf('italic_toggle')>=0){ var sourceElem=$(evt.target); sourceElem.toggleClass('fa-chevron-circle-down'); sourceElem.toggleClass(“fa-chevron-circle-up”); var targetElem=$('#'+evt.target.id.replace('italic','div'); 目标元素滑动切换(600); } } });,javascript,jquery,ember.js,handlebars.js,Javascript,Jquery,Ember.js,Handlebars.js,路由、控制器和模型定义如下: Portal.ProfilesRoute = Ember.Route.extend({ 'setupController': function(controller, model) { controller.set('model', model); }, 'model': function(params) { return this.store.find('profile', params.userId);
Portal.ProfilesRoute = Ember.Route.extend({
'setupController': function(controller, model) {
controller.set('model', model);
},
'model': function(params) {
return this.store.find('profile', params.userId);
},
'actions': {
'didTransition': function() {
setTimeout(function() {
$("#input-dob").inputmask("dd/mm/yyyy", {"placeholder": "dd/mm/yyyy"});
}, 250);
}
}
});
Portal.ProfilesController = Ember.ObjectController.extend({
'actions': {
'addContact': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('contacts').addObject(this.store.createRecord('contact', {
'id': newid,
'dbid': newid
}));
},
'deleteContact': function(id) {
var user = this.get('model');
this.store.find('contact', id)
.then(function(contact) {
user.get('contacts').removeObject(contact);
contact.deleteRecord();
});
},
'addAddress': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('addresses').addObject(this.store.createRecord('address', {
'id': newid,
'dbid': newid
}));
},
'deleteAddress': function(id) {
var user = this.get('model');
this.store.find('address', id)
.then(function(address) {
user.get('addresses').removeObject(address);
address.deleteRecord();
});
},
'addEmergencyContact': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('emergencyContacts').addObject(this.store.createRecord('emergencyContact', {
'id': newid,
'dbid': newid
}));
},
'deleteEmergencyContact': function(id) {
var user = this.get('model');
this.store.find('emergencyContact', id)
.then(function(emergencyContact) {
user.get('emergencyContacts').removeObject(emergencyContact);
emergencyContact.deleteRecord();
});
},
'save': function() {
this.model.save();
},
'cancel': function() {
this.model.get('contacts').content.invoke('rollback');
this.model.get('addresses').content.invoke('rollback');
this.model.rollback();
}
}
});
Portal.Profile = DS.Model.extend({
'firstName': DS.attr('string'),
'middleNames': DS.attr('string'),
'lastName': DS.attr('string'),
'suffix': DS.attr('string'),
'dob': DS.attr('date'),
'gender': DS.attr('string'),
'contacts': DS.hasMany('contact'),
'addresses': DS.hasMany('address'),
'emergencyContacts': DS.hasMany('emergencyContact'),
'formattedDoB': function(key, value, prevValue) {
if(arguments.length <= 1) {
return moment(this.get('dob')).format('DD/MM/YYYY');
}
// Set the new DOB
var newDoB = moment(value, 'DD/MM/YYYY');
if(newDoB.isValid()) {
this.set('dob', newDoB.toDate());
}
}.property('dob'),
'genders': ['Male', 'Female']
});
Portal.Contact = DS.Model.extend({
'dbid': DS.attr('string'),
'type': DS.attr('string'),
'code': DS.attr('string'),
'number': DS.attr('string'),
'contactTypes': ['Home Landline', 'Office Landline', 'Other Landline', 'Personal Mobile', 'Office Mobile', 'Other Mobile', 'Other'],
'countryCodes': ['1', '91']
});
Portal.Address = DS.Model.extend({
'dbid': DS.attr('string'),
'type': DS.attr('string'),
'line1': DS.attr('string'),
'line2': DS.attr('string'),
'line3': DS.attr('string'),
'area': DS.attr('string'),
'city': DS.attr('string'),
'state': DS.attr('string'),
'country': DS.attr('string'),
'pincode': DS.attr('string'),
'addressTypes': ['Home', 'Office', 'Other'],
'countries': ['India', 'USA'],
'states': function() {
var countryStates = {
'India': ['Andhra Pradesh', 'Telangana'],
'USA': ['Alabama', 'Kentucky']
};
return countryStates[this.get('country')];
}.property('country'),
'cities': function() {
var stateCities = {
'Andhra Pradesh': ['Vijayawada', 'Guntur', 'Tirupati'],
'Telangana': ['Hyderabad', 'Secunderabad']
};
return stateCities[this.get('state')];
}.property('state')
});
Portal.EmergencyContact = DS.Model.extend({
'dbid': DS.attr('string'),
'code': DS.attr('string'),
'number': DS.attr('string'),
'countryCodes': ['1', '91']
});
Portal.ProfilesRoute=Ember.Route.extend({
“setupController”:函数(控制器、模型){
controller.set('model',model);
},
“模型”:函数(参数){
返回this.store.find('profile',params.userId);
},
“行动”:{
“didTransition”:函数(){
setTimeout(函数(){
$(“#输入dob”).inputmask(“dd/mm/yyyy”,“占位符”:“dd/mm/yyyy”});
}, 250);
}
}
});
Portal.ProfilesController=Ember.ObjectController.extend({
“行动”:{
“addContact”:函数(){
var user=this.get('model'),
newid=Portal.generateuid();
user.get('contacts').addObject(this.store.createRecord('contact'),{
“id”:newid,
“dbid”:newid
}));
},
“deleteContact”:函数(id){
var user=this.get('model');
this.store.find('contact',id)
.然后(功能(联系人){
user.get('contacts').removeObject(contact);
contact.deleteRecord();
});
},
“addAddress”:函数(){
var user=this.get('model'),
newid=Portal.generateuid();
user.get('addresses').addObject(this.store.createRecord('address'),{
“id”:newid,
“dbid”:newid
}));
},
“deleteAddress”:函数(id){
var user=this.get('model');
this.store.find('address',id)
.then(功能(地址){
user.get('addresses').removeObject(address);
address.deleteRecord();
});
},
“AddMergencyContact”:函数(){
var user=this.get('model'),
newid=Portal.generateuid();
user.get('emergencyContacts').addObject(this.store.createRecord('emergencyContacts'),{
“id”:newid,
“dbid”:newid
}));
},
“deleteEmergencyContact”:函数(id){
var user=this.get('model');
this.store.find('emergencyContact',id)
.然后(功能(紧急联系人){
user.get('emergencyContacts').removeObject(emergencyContact);
emergencyContact.deleteRecord();
});
},
“保存”:函数(){
this.model.save();
},
“取消”:函数(){
this.model.get('contacts').content.invoke('rollback');
this.model.get('addresses').content.invoke('rollback');
this.model.rollback();
}
}
});
Portal.Profile=DS.Model.extend({
'firstName':DS.attr('string'),
“中间名”:DS.attr('string'),
'lastName':DS.attr('string'),
“后缀”:DS.attr('string'),
'dob':DS.attr('date'),
“性别”:DS.attr('string'),
“联系人”:DS.hasMany(“联系人”),
“地址”:DS.hasMany(“地址”),
'emergencyContacts':DS.hasMany('emergencyContact'),
“formattedDoB”:函数(键、值、prevValue){
if(arguments.length这里是我从代码中发现的两件事
第一件事:
你有你的余烬路线混合在一起。
您应该清楚路由中的单数/复数名称约定
Portal.ProfilesRoute指的是一组配置文件。
Portal.ProfileRoute指1个单独的配置文件
因为您在Portal.ProfilesRoute中定义了这两种路由。第一次单击“Ember”时,将自动路由假定您返回相同的模型,因此甚至不会调用您的路由。但是,当您尝试导航回模型挂钩实际调用您的路由时,最终会出现错误
第二件事
和第一件事有点关系:
'setupController': function(controller, model) {
controller.set('model', model);
},
'model': function(params) {
return this.store.find('profile', params.userId);
}
一个但不是两个。例如,如果要对模型执行某些操作,请使用setupController
'setupController': function(controller, model) {
//do something to the model if you want to
// maybe change some attribute
model.set('name','blah blah');
controller.set('model', model);
},
否则就做吧
'model': function(params) {
return this.store.find('profile', params.userId);
},
但是,是的,正如在第1件事中提到的,这应该是单数路径,也就是ProfileRoute
希望有帮助
<script type="text/x-handlebars" data-template-name="emergency-contact-information">
<div class="box box-primary">
<div class="box-header">
<div class="pull-left"><h3 class="box-title">Emergency Contacts</h3></div>
<div class="pull-right box-tools"><button type="button" class="btn btn-default btm-sm" {{action "addEmergencyContact"}}>+</button></div>
</div>
<form role="form">
{{#each emergencyContacts}}
<div class="box-body bg-gray" style="margin-bottom:5px;">
<div class="form-group">
<div class="pull-left">
{{input type="text" class="form-control" placeholder="Contact Name" value=name}}
</div>
<div class="pull-right box-tools">
<a><i id="italic_toggle_emergency_contact_{{unbound id}}" class="fa fa-chevron-circle-down fa-2x text-blue"></i></a>
<a><i class="fa fa-trash-o fa-2x text-orange" {{action "deleteEmergencyContact" id}}></i></a>
</div>
<div> </div>
</div>
<div id="div_toggle_emergency_contact_{{unbound id}}" class="form-group" style="display:none;">
<div class="pull-left" style="margin-top:3px;">
{{view Ember.Select class="form-control" content=countryCodes value=code}}
</div>
<div class="pull-left" style="margin-top:3px;">
{{input type="text" class="form-control" value=number}}
</div>
<div> </div>
</div>
</div>
{{/each}}
</form>
</div>
</script>
Portal.EmergencyContactInformationView = Ember.View.create({
'templateName': 'emergency-contact-information',
'click': function(evt) {
// Step #1: Stop the event from bubbling...
evt.preventDefault();
evt.stopPropagation();
// Step #2: If this is for collapsing / expanding contacts...
if(evt.target.id.indexOf('italic_toggle') >= 0) {
var sourceElem = $(evt.target);
sourceElem.toggleClass('fa-chevron-circle-down');
sourceElem.toggleClass('fa-chevron-circle-up');
var targetElem = $('#' + evt.target.id.replace('italic', 'div'));
targetElem.slideToggle(600);
}
}
});
Portal.ProfilesRoute = Ember.Route.extend({
'setupController': function(controller, model) {
controller.set('model', model);
},
'model': function(params) {
return this.store.find('profile', params.userId);
},
'actions': {
'didTransition': function() {
setTimeout(function() {
$("#input-dob").inputmask("dd/mm/yyyy", {"placeholder": "dd/mm/yyyy"});
}, 250);
}
}
});
Portal.ProfilesController = Ember.ObjectController.extend({
'actions': {
'addContact': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('contacts').addObject(this.store.createRecord('contact', {
'id': newid,
'dbid': newid
}));
},
'deleteContact': function(id) {
var user = this.get('model');
this.store.find('contact', id)
.then(function(contact) {
user.get('contacts').removeObject(contact);
contact.deleteRecord();
});
},
'addAddress': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('addresses').addObject(this.store.createRecord('address', {
'id': newid,
'dbid': newid
}));
},
'deleteAddress': function(id) {
var user = this.get('model');
this.store.find('address', id)
.then(function(address) {
user.get('addresses').removeObject(address);
address.deleteRecord();
});
},
'addEmergencyContact': function() {
var user = this.get('model'),
newid = Portal.generateUUID();
user.get('emergencyContacts').addObject(this.store.createRecord('emergencyContact', {
'id': newid,
'dbid': newid
}));
},
'deleteEmergencyContact': function(id) {
var user = this.get('model');
this.store.find('emergencyContact', id)
.then(function(emergencyContact) {
user.get('emergencyContacts').removeObject(emergencyContact);
emergencyContact.deleteRecord();
});
},
'save': function() {
this.model.save();
},
'cancel': function() {
this.model.get('contacts').content.invoke('rollback');
this.model.get('addresses').content.invoke('rollback');
this.model.rollback();
}
}
});
Portal.Profile = DS.Model.extend({
'firstName': DS.attr('string'),
'middleNames': DS.attr('string'),
'lastName': DS.attr('string'),
'suffix': DS.attr('string'),
'dob': DS.attr('date'),
'gender': DS.attr('string'),
'contacts': DS.hasMany('contact'),
'addresses': DS.hasMany('address'),
'emergencyContacts': DS.hasMany('emergencyContact'),
'formattedDoB': function(key, value, prevValue) {
if(arguments.length <= 1) {
return moment(this.get('dob')).format('DD/MM/YYYY');
}
// Set the new DOB
var newDoB = moment(value, 'DD/MM/YYYY');
if(newDoB.isValid()) {
this.set('dob', newDoB.toDate());
}
}.property('dob'),
'genders': ['Male', 'Female']
});
Portal.Contact = DS.Model.extend({
'dbid': DS.attr('string'),
'type': DS.attr('string'),
'code': DS.attr('string'),
'number': DS.attr('string'),
'contactTypes': ['Home Landline', 'Office Landline', 'Other Landline', 'Personal Mobile', 'Office Mobile', 'Other Mobile', 'Other'],
'countryCodes': ['1', '91']
});
Portal.Address = DS.Model.extend({
'dbid': DS.attr('string'),
'type': DS.attr('string'),
'line1': DS.attr('string'),
'line2': DS.attr('string'),
'line3': DS.attr('string'),
'area': DS.attr('string'),
'city': DS.attr('string'),
'state': DS.attr('string'),
'country': DS.attr('string'),
'pincode': DS.attr('string'),
'addressTypes': ['Home', 'Office', 'Other'],
'countries': ['India', 'USA'],
'states': function() {
var countryStates = {
'India': ['Andhra Pradesh', 'Telangana'],
'USA': ['Alabama', 'Kentucky']
};
return countryStates[this.get('country')];
}.property('country'),
'cities': function() {
var stateCities = {
'Andhra Pradesh': ['Vijayawada', 'Guntur', 'Tirupati'],
'Telangana': ['Hyderabad', 'Secunderabad']
};
return stateCities[this.get('state')];
}.property('state')
});
Portal.EmergencyContact = DS.Model.extend({
'dbid': DS.attr('string'),
'code': DS.attr('string'),
'number': DS.attr('string'),
'countryCodes': ['1', '91']
});
'setupController': function(controller, model) {
controller.set('model', model);
},
'model': function(params) {
return this.store.find('profile', params.userId);
}
'setupController': function(controller, model) {
//do something to the model if you want to
// maybe change some attribute
model.set('name','blah blah');
controller.set('model', model);
},
'model': function(params) {
return this.store.find('profile', params.userId);
},