I have a large list of items. Each item has it's own details.
In my main view/partial, I simply display a large list list of the item names.
When the user clicks on an item, I want the page to go to a partial which works as a "template", displaying information based on which list item is clicked, and hence possibly what the URL looks like. E.g. /listItem1/
This diagram below hopefully sums up what I want to achieve pretty clearly.
How can I do this?
Right now, I have a pretty standard set up in which I have all the information for each list item in an array of object literals, which is contained in a controller injected into the main app module. Like so:
var app = angular.module('app', [/*nodependencies*/]);
var controllers = {};
app.controller(controllers);
controllers.listController = function ($scope){
$scope.list = [
{name: 'List Item 1 Name', detail1: 'blahblah1', detail2: 'blahblah2'},
{name: 'List Item 2 Name', detail1: 'blahblah1', detail2: 'blahblah2'},
{name: 'List Item 3 Name', detail1: 'blahblah1', detail2: 'blahblah2'}
..... and so on
I know how to create basic views/partials as well. But what would be my next steps?
You can do what you want, using the built-in router which ships with AngularJS.
var app = angular.module('app', [/*nodependencies*/])
.config(function($routeProvider) {
$routeProvider
.when('/:itemId', {
templateUrl: '/path/to/partial',
controller : function($scope, $routeParams) {
$scope.item = $routeParams.itemId;
}
})
});
Basically, what the above means, is that if you browse to pdf/item/1
Then you will have access in your controller to $routeParams.itemId which will be equal to 1. You can then do whatever logic is necessary with this information on your partial to show the information you want.
Hope this helps.
Update
Please look at the controller, this is how you would get the param you passed via the URL, you would then do whatever it is you need to do with that param in the controller, and pass the data back to the view.
You can create a small directive that will use the multi-use partial to display each item on the list
Take a look at this working example (http://plnkr.co/edit/0jNVxRg6g3p8uxpustzz?p=preview)
var myApp = angular.module('myApp', []);
myApp.controller('listController', ['$scope', function ($scope) {
$scope.list = [
{
name: 'List Item 1 Name',
url: 'pdfs/item1.pdf',
detail: 'blahblah'
},
{
name: 'List Item 2 Name',
url: 'pdfs/item2.pdf',
detail: 'blahblah'
},
{
name: 'List Item 3 Name',
url: 'pdfs/item3.pdf',
detail: 'blahblah'
}
];
$scope.selectItem = function(item){
$scope.selected = item;
}
}]);
myApp.directive('listItem', [function () {
return {
restrict: 'A',
scope: {
item: '='
},
templateUrl: 'multiple-partial.html',
link: function (scope, element, iAttrs) {
}
};
}])
Related
I have a simple User model, with two fields: name and user_id
When the app starts, I want to check if my User model contains at least one record - if not, I want to create a default "admin" user automatically.
I have the following code in my application route:
model() {
let user = this.store.findAll('user'); // find all users
let record = user.then(function(result) {
let first = result.get('firstObject'); // check if we at least one user
if (first) { // if true, then we have at least one user
let user_id = first.get('user_id'); // get the first user's id
// do some more stuff here
}
else {
console.log("no record found"); // no user found, let's create our default user
let user = this.store.createRecord("user", { // <-- THIS DOESNT WORK - where can I create my record?
user_id: 1,
name: 'admin'
});
}
});
}
This code works, except that I can't call this.store inside my promise function, since this now belongs to the function.
Would it be better to create an action, and then call that action in my else statement? But then, how would I trigger that action from the model itself?
Any ideas would be helpful at this point. It feels like I'm misunderstanding some fundamental law of Ember (again).
SOLUTION:
Annnnd it was missing an arrow.. this seems to preserve the this context, through some dark ECMAscript magic
let uniqueid = records.then(function(result) { // this returns an error (TypeError: this is undefined)
let uniqueid = this.store.createRecord("uniqueid", {
user_id: 1,
name: 'admin'
});
}
let uniqueid = records.then((result) => { // this works!
let uniqueid = this.store.createRecord("uniqueid", {
user_id: 1,
name: 'admin'
});
}
Thanks #sheriffderek !
I think that this mystery is a JavaScript mystery more than an Ember one.
jsFiddle just broke... and I'm giving up on my example BUT... I think that if you just use es2015 arrow functions, they won't rebind the 'this'
something
.then( (response)=> {
// ?
})
;
OR you can do a pre 2015 style var context = this; somewhere in the outer scope and then use that in the inner scope. Give it a try! : )
I am using Ionic 2 rc4. I am following the advise here and am trying to do the following:
import { NavController } from 'ionic-angular';
...
this.nav.present(this.loading).then(() => {
However, to me it looks like the NavController does not have a present function, because I get:
[ts] Property 'present' does not exist on type 'NavController'.
any
Am I correct, or am I doing something wrong? How do they get to access this "phantom" function?
Any advise appreciated.
UPDATE
Here is my code that results in the following error (on this.loading.present().then(() => {):
"Cannot read property 'nativeElement' of null"
It presents loading the first time. but after the alert is presented if submit() is run again, it gets this error.
submit() {
this.loading.present().then(() => {
let alert = this.alertCtrl.create({
title: 'Verify Email',
subTitle: 'Please verify your email address before you log in.',
message: 'Check your Spam folder if you cannot find the email.',
buttons: [
{
text: 'Resend',
handler: data => {
firebaseUser.sendEmailVerification().then((data) => {
this.doAlert('Verify Email', 'Verification Email Sent.').then((data) => {
//navCtrl.setRoot(navCtrl.getActive());
});
});
}
},
{
text: 'Okay',
handler: data => {
//navCtrl.setRoot(navCtrl.getActive());
}
}
]
});
alert.present();
this.loading.dismiss();
});
}
Looking at this changelog for Beta 11
They have removed present function from Navcontroller.
You need to refactor your code and use some other function based on your requirement.
this.loading.present()
For the error, check the Loading controller docs.
Note that after the component is dismissed, it will not be usable
anymore and another one must be created. This can be avoided by
wrapping the creation and presentation of the component in a reusable
function
Just do :
this.loading = this.loadingCtrl.create({
//loading properties
});
inside submit() before this.loading.present()
I am accessing model data using the .find method but how to get records in JSON format from the model? I am getting output from .find() as: (Console log view)
Class {type: function, store: Class, isLoaded: true, isUpdating: true, toString: function…}
ember1375269653627: "ember313" __ember1375269653627_meta: Meta _super: undefined get content: function () { isLoaded: true isUpdating: false set content: function (value) { store: Class toString: function () { return ret; } type: Grid.ModalModel __proto: Object
I am new user of this community, so unable to upload image.
If you're using Ember Model, you do model.toJSON(). If you are trying to get values from the model you should use the getter model.get('name').
In javascript to create a JSON out of javascript object you may want to use:
JSON.stringify({name: "John"}); // => "{"name":"John"}"
It works pretty good for plain Ember.Objects. But you may not want to stringify all properties of given object. In such case you should use getProperties method of EmberObject. For example:
var john = Ember.Object.create({firstName: "John", lastName: "Doe", title: "CEO"});
JSON.stringify(john); // => "{"firstName":"John","lastName":"Doe", "title": "CEO"}"
var namesOnly = john.getProperties("firstName","lastName");
JSON.stringify(namesOnly); // => "{"firstName":"John","lastName":"Doe"}"
I've been trying to create an object to use in a jtable as the options (for a select list).
I don't seem to have the format correct. The jtable.org website says it will take an array:
From the jtable.org website:
http://jtable.org/ApiReference#fopt-options
PhoneType: {
title: 'Phone type',
options: [{ Value: '1', DisplayText: 'Home phone' }, { Value: '2', DisplayText: 'Office phone' }, { Value: '2', DisplayText: 'Cell phone' }]
}
However, when I create an object like that:
var optionsObject = [];
optionsObject.push({Value: i, DisplayText: 'Hello' + i});
and then use it as a variable for the options in my jtable:
key: true,
options: optionsObject,
I don't get the items in the select list drop down. I do get something in the select list, but that looks like '[object Object]' instead of the actual items.
I'm not really sure what I'm doing wrong.
If I send an object that looks like this:
object.push('hello' + i);
I will get an item in the select list that looks like this 'hello0', as expected, but then the display text is also used as the value. I need to have an object with separate display texts and values.
Has anybody had any success with doing this?
After much trial and error, and debugger stops in the jtable scripting files, I learned that in order to use an object for the 'options' property of a field in jtable, the object must be in the following format
var optionsObject = new Object();
optionsObject[Value] = DisplayText;
I'm new to Sencha Touch and the Ext JS logic. Actually, I'm displaying a list of items containing two fields (title, type) grouped by default alphabetically by first character of the title and I added a button to a toolbar that I want it to switch the grouping to be by type. I tried to programmatically set the getGroupString function in the buton handler in this way :
var toolbar = new Ext.Toolbar({
dock: 'top',
title: 'Toolbar',
items: [{
text: 'By type',
handler: function() {
MyApp.MyStore.getGroupString = function(record) {
return record.get('type');
};
MyApp.itemsList.update();
}
}]
});
But that seems just to not work. Any help ?
Thanks
Just wanted to close the question as I have already answered it
MyApp.itemsList.refresh();