Meteor angular template function call - angularjs

I have been having problems rendering a template function call from a meteor/angular template. I am trying to use the Moment.js package to format my time stamp. I have seen examples of this done with Blaze however, I can't replicate it in Meteor with Angular. My issue is with the formatTimestamp() function call. It isn't rendering anything and there are no errors in my console. Where am I going wrong?
My template
<li ng-repeat="task in $ctrl.tasks" ng-class="{'private' : task.private}">
<details>
<summary>{{task.administration.inventoryNumber}}</summary>
<ul class="bxslider">
<li><h3>Adminstration</h3>
<table>
<td>created at: </td><td>{{formatTimestamp(task.administration.createdAt)}}</td>
My controller
class TodosListCtrl {
constructor($scope) {
$scope.viewModel(this);
this.subscribe('tasks');
this.helpers({
tasks() {
const selector = {};
return Artefacts.find(selector, {
sort: {
createdAt: -1
}
});
},
currentUser() {
return Meteor.user();
},
formatTimestamp: function (timestamp) {
alert("timestamp");
console.log(timestamp);
return moment(timestamp).calendar();
}
})
}
Insert Mongo function
Artefacts.insert({
administration: {
inventoryNumber: invNum,
boxNumber: boxNum,
createdAt: new Date(),

Your custom function goes outside of the constructor, like this
class TodosListCtrl {
constructor($scope) {
$scope.viewModel(this);
this.subscribe('tasks');
this.helpers({
tasks() {
const selector = {};
return Artefacts.find(selector, {
sort: {
createdAt: -1
}
});
},
currentUser() {
return Meteor.user();
}
})
}
formatTimestamp(timestamp) {
alert("timestamp");
console.log(timestamp);
return moment(timestamp).calendar();
}
}
But also, remember in the view to call formatTimeStamp with $ctrl as prefix because it looks like you are using the ControllerAs syntax.
Your code:
<li ng-repeat="task in $ctrl.tasks" ng-class="{'private' : task.private}">
<details>
<summary>{{task.administration.inventoryNumber}}</summary>
<ul class="bxslider">
<li><h3>Adminstration</h3>
<table>
<td>created at: </td><td>{{formatTimestamp(task.administration.createdAt)}}</td>
How it should be:
<li ng-repeat="task in $ctrl.tasks" ng-class="{'private' : task.private}">
<details>
<summary>{{task.administration.inventoryNumber}}</summary>
<ul class="bxslider">
<li><h3>Adminstration</h3>
<table>
<td>created at: </td><td>{{$ctrl.formatTimestamp(task.administration.createdAt)}}</td>

don't need to create a function for date format. It is bad practice to call a function with angular expressions {{}}. You can format your date in Meteor helper.
this.helpers({
tasks() {
const selector = {};
this.value = Artefacts.find(selector, {
sort: {
createdAt: -1
}
// add filter here on createdAt
$filter('filter')(this.value.cretedAt);
});
},

Related

React Cannot read property '__reactInternalInstance$2f71vks24hx' of null [duplicate]

Here is the problematic component in question.
const UserList = React.createClass({
render: function(){
let theList;
if(this.props.data){
theList=this.props.data.map(function(user, pos){
return (
<div className="row user">
<div className="col-xs-1">{pos}</div>
<div className="col-xs-5">{user.username}</div>
<div className="col-xs-3">{user.recent}</div>
<div className="col-xs-3">{user.alltime}</div>
</div>
);
}, this);
} else {
theList = <div>I don't know anymore</div>;
}
console.log(theList);
return (
theList
);
}
});
Whenever I attempt to return {theList}, I receive a Cannot read property '__reactInternalInstance$mincana79xce0t6kk1s5g66r' of null error. However, if I replace {theList} with static html, console.log prints out the correct array of objects that i want. As per the answers, I have tried to return both {theList} and theList but that didn't help.
In both cases, console.log first prints out [] which I assume is because componentDidMount contains my ajax call to get json from the server and has not fired yet before the first render(). I have tried to check against
this.props.data being null but it does not help.
Here is the parent component if it helps:
const Leaderboard = React.createClass({
getInitialState: function(){
return ({data: [], mode: 0});
},
componentDidMount: function(){
$.ajax({
url: 'https://someurlthatreturnsjson',
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error('https://someurlthatreturnsjson', status, err.toString());
}.bind(this)
});
},
render: function(){
return (
<div className="leaderboard">
<div className="row titleBar">
<img src="http://someimage.jpg"></img>Leaderboard
</div>
<HeaderBar />
<UserList data={this.state.data}/>
</div>
);
}
});
Ah OK, there were some interesting problems in here, but you were so close. The big one, with react you must always return a single top-level element (e.g. a div). So, your variable theList was actually an array of divs. You can't return that directly. But you can return it if it's wrapped in a single parent div.
const mockData = [
{
username: 'bob',
recent: 'seven',
alltime: 123,
},
{
username: 'sally mae',
recent: 'seven',
alltime: 133999,
},
];
var $ = {
ajax(opt) {
setTimeout(() => {
opt.success(mockData);
}, 200);
}
}
const UserList = React.createClass({
render: function(){
let theList;
if (this.props.data && this.props.data.length) {
theList = this.props.data.map(function(user, pos){
return (
<div key={user.username} className="row user">
<div className="col">{pos}</div>
<div className="col">{user.username}</div>
<div className="col">{user.recent}</div>
<div className="col">{user.alltime}</div>
</div>
);
});
} else {
theList = <div>There is NO data</div>;
}
return <div>{theList}</div>;
}
});
const Leaderboard = React.createClass({
getInitialState: function(){
return ({data: [], mode: 0});
},
componentDidMount: function(){
$.ajax({
url: 'https://someurlthatreturnsjson',
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error('https://someurlthatreturnsjson', status, err.toString());
}.bind(this)
});
},
render: function(){
return (
<div className="leaderboard">
<UserList data={this.state.data}/>
</div>
);
}
});
ReactDOM.render(
<Leaderboard/>,
document.getElementById('container')
);
.col {
width: 200px;
float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
To explain the fiddle a little bit. Don't worry about the weird looking var $ stuff, I'm just stubbing out jQuery's ajax method so I can return some fake data after 200ms.
Also, for me jsfiddle gives me a 'bad config' message when I run it, but I close the message and the result is there. Don't know what that's about.
return (
{theList}
)
should just be
return theList
because you are not inside JSX at that point. What you're doing there will be interpreted as
return {
theList: theList
}
That's ES6 shorthand properties syntax.
Error can also arise from accessing nested state that doesn't exist:
I lack the reputation to comment, so adding an answer for future assistance -- I ran into this same issue for a different reason. Apparently, the error is triggered from an earlier error throwing off react's internal state, but the error is getting caught somehow. github issue #8091
In my case, I was trying access a property of state that didn't exist after moving the property to redux store:
// original state
state: {
files: [],
user: {},
}
// ... within render function:
<h1> Welcome {this.state.user.username} </h1>
I subsequently moved user to redux store and deleted line from state
// modified state
state: {
files: [],
}
// ... within render function (forgot to modify):
<h1> Welcome {this.state.user.username} </h1>
And this threw the cryptic error. Everything was cleared up by modifying render to call on this.props.user.username.
There is a small problem with the if statement:
if(this.props.data !== []){
should be:
if(this.props.data){
this.props.data is null, if the ajax call returns null. alternatively the code could be more elaborate.
const data = this.props.data;
if(data && data.constructor === Array && data.length > 0) {
Not sure if this is how you want to do it, but it works for me.
edit:
const UserList = React.createClass({
render: function() {
if(this.props.data){
return this.props.data.map(function(user, pos){
return (
<li> className="row user">
<span>{pos}</span>
<span>{user.username}</span>
<span>{user.recent}</span>
<span>{user.alltime}</span>
</li>
);
});
} else {
return <li>I don't know anymore</li>;
}
}
});
I encountered this error when I rendered a stateless component and decided to remove the react-root-element (wrapping div) after rendering it with basic dom manipulation.
Conclusion: be aware of this, better don't do it.

AngularJs Component and angularjs-dropdown-multiselect

I am new to AngularJs world and was trying to use angularjs-dropdown-multiselect inside component.
Component's HTML looks like:
<div>
<select id="statusSelection" class="form-control"
ng-options="status.name for status in $ctrl.statuses track by status.id"
ng-model="$ctrl.status" ng-change="$ctrl.filterChanged()"></select>
</div>
<div ng-dropdown-multiselect="" options="$ctrl.categories" selected-model="$ctrl.category"
events="$ctrl.events">
</div>
Event on status changed and category will call the same action.
MultiselectController.prototype.filterChanged = function() {
this.onFilterChange({
status: this.status,
category: this.category});
};
MultiselectController.prototype.events = {
onItemSelect: function(item) {
filterChanged();
},
onItemDeselect: function(item) {
filterChanged();
}
};
When I try to run the above code and change the status, the code works as expected but fails during Category change(error in console).
Error message: ReferenceError: filterChanged is not defined
Angular version: 1.5.8
angularjs-dropdown-multiselect: 1.11.8
Plunker: http://plnkr.co/edit/JL7N6M?p=preview
Thanks for helping me out here.
I have created an instance variable and initialize it to instance of Controller.
var _instance;
function MultiselectController($scope) {
this.statuses = testMultiselect.statuses;
this.categories = testMultiselect.categories;
this.$scope = $scope;
this.setDefault();
_instance = this;
}
Now, I am using this instance variable to access the functions on Controller.
MultiselectController.prototype.events = {
onItemSelect: function(item) {
_instance.filterChanged();
},
onItemDeselect: function(item) {
_instance.filterChanged();
}
};
I am not completely happy with this as there should be better way to do the same but until I find, I will keep this.
Updated Plunker: http://plnkr.co/edit/D7BKI9?p=preview

How to get a data of object from Firebase 'key':'true' patern

I have this firebase data:
{
"userData": {
"user01": {
"name": "User One"
"provider": "provider0001"
},
"user02": {
"name": "User Two"
"provider": "provider0001"
},
"user03": {
"name": "User Three"
"provider": "provider0002"
}
},
"provider": {
"provider0001": {
"users": {
"user01": true,
"user02": true
}
},
"provider0002": {
"users": {
"user03": true
}
}
}
}
controller
vm.provider = $firebaseObject(ref.child('provider').child('provider0001'));
simple html
<ul>
<li ng-repeat="user in $ctrl.provider.users">
{{user}}
</li>
</ul>
Why I cannot list {{user}} as object? The above will display list of true's, but how can I access the user object it self?
You need to map the keys (your userId's) in the users-object, to the values in your userData-object. You can get the keys in an ng-repeat-directive like this:
ng-repeat="(key, value) in $ctrl.provider.users"
Then in your controller you need to add the userData object to your controller scope:
vm.userData = $firebaseObject(ref.child('userdata'));
So you can implement your ng-repeat like this:
<ul>
<li ng-repeat="(key, value) in $ctrl.provider.users"
ng-init="user = $ctrl.userData[key]">
{{user}}
</li>
</ul>
With that said, you probably ought to do this mapping when you retrieve the data in the controller initially. Something like this:
function loadData(){
var providerUsers = $firebaseObject(ref.child('provider').child('provider0001').child('users'));
var userData = $firebaseObject(ref.child('userData'));
vm.users = [];
angular.forEach(providerUsers, function(value, key) {
vm.users.push(userData[key]);
});
}
Then you can just iterate using ng-repeat over this new vm.users-array.
Update
Here's a solution where you only retrieve the actual mapped users. It makes a query for each user, since I couldn't find a decent way to join two collections by key using just a single Firebase query.
function loadData(){
var userRef = ref.child('provider').child('provider0001').child('users');
var userDataRef = ref.child('userData');
vm.users = [];
userRef.once('value', function(snapshot){
snapshot.forEach(function(userId){
userDataRef.child(userId.key()).once("value", function(user){
$scope.$apply(function () {
vm.users.push(user.val());
});
});
})
});
}
My familiarity with Firebase is very limited so there might be better solutions out there
<ul>
<li ng-repeat="(key,user) in $ctrl.provider.users">
{{key}}:{{user}}
</li>
key will give the key of the user and user will give value

How to render array passed to sub element in React JS?

I have simple example with JSON that has array in it.
It is loaded with ajax:
{somekey: "value1", somekey2: "value2", belongsto: ["me","you"]}
I can render it by for example:
<div>
belongsto: {this.state.data.belongsto}
</div>
But I would love to render it as list in subcomponent, so I am trying:
<Uli data={this.state.data.belongsto}/>
as:
var Uli = React.createClass({
render: function() {
return (
<ul className="Uli">
{
this.props.data.map(function(value) {
return <li key={value}>{value}</li>
})
}
</ul>
)
}
});
And that gives me:
Uncaught TypeError: Cannot read property 'map' of undefined
How this should be achieved?
You are loading your json data through AJAX asynchronously, and hence belongsto is undefined until you'll got the response from the server.
There are several solutions to solve this:
Add getDefaultProps method to your ULi component.
var Uli = React.createClass({
getDefaultProps() {
return {
data: []
}
},
render: function() {
return (
<ul className="Uli">
{this.props.data.map(function(value) {
return <li key={value}>{value}</li>
})}
</ul>
)
}
});
Use || operator:
<ul className="Uli">
{(this.props.data || []).map(function(value) {
return <li key={value}>{value}</li>
})}
</ul>
Prevent ULi rendering, if does not belongsto is undefined:
{this.state.data.belongsto && <Uli data={this.state.data.belongsto}/>}

adding more than one sources of data to angular

this is what I have in my model
// The contents of individual model .js files will be concatenated into dist/models.js
(function() {
// Protects views where angular is not loaded from errors
if ( typeof angular == 'undefined' ) {
return;
};
var module = angular.module('myModel', ['restangular']);
module.factory('myRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost/data');
RestangularConfigurer.setRequestSuffix('.json');
RestangularConfigurer.setRestangularFields({
id: "my_id"
});
});
});
})();
this is fine. but now I have another json that I need to grab data from. How could I change this model to look for that other json as well. I am very very new to angular and still learning how model data binding works!
*This is what I have tired *
my model
var module = angular.module('FloorModel', ['restangular']);
module.factory('FloorRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://localhost/data/floor');
RestangularConfigurer.setRequestSuffix('.json');
RestangularConfigurer.setRestangularFields({
id: "floor_id"
});
});
});
**my controller**
myApp.controller('FloorCtrl', function ($scope, $filter, FloorRestangular) {
// Fetch all objects from the local JSON (see app/models/mdpocket.js)
FloorRestangular.all('floor').getList().then( function(floors) {
// Then select the one based on the view's id query parameter
$scope.floor = $filter('filter')(floors, {floor_id: steroids.view.params['id']})[0];
});
// -- Native navigation
steroids.view.navigationBar.show("Floor: " + steroids.view.params.id );
});
*my view *
<div ng-app="myApp">
<div ng-controller="FloorCtrl">
<div class="topcoat-list__container">
<ul class="topcoat-list">
<li class="topcoat-list__item" hm-tap="open(floor.floor_id)" ng-repeat="floor in floors">
Floor Name: {{ floor.name }}
</li>
</ul>
</div>
</div>
</div>

Resources