I have a helper like so:
import Ember from 'ember';
export function bootstrapModelStatus(model) {
if(model.get('isDeleted')) {return 'danger'};
if(model.get('isError')) { return 'danger'};
if(!model.get('isValid')) { return 'warning'};
if(model.get('isSaving')) { return 'info'};
if(model.get('isNew')) { return 'default'};
if(model.get('isDirty')) { return 'warning'};
if(model.get('isLoaded')) { return 'success'};
return 'error';
}
export default Ember.Handlebars.makeBoundHelper(bootstrapModelStatus);
And it is bound to a DS.Model like so:
{{bootstrap-model-status model}}
The trouble is that the above does not update when the state of the model changes. Instead I had to do the following as a bit of a hack.
{{bootstrap-model-status model model.currentState.stateName}}
Is there a better way of doing this?
You can tell the helper to redisplay when a property of the model you passed in changes - see here
In your case, that would mean the following:
import Ember from 'ember';
export function bootstrapModelStatus(model) {
if(model.get('isDeleted')) {return 'danger'};
if(model.get('isError')) { return 'danger'};
if(!model.get('isValid')) { return 'warning'};
if(model.get('isSaving')) { return 'info'};
if(model.get('isNew')) { return 'default'};
if(model.get('isDirty')) { return 'warning'};
if(model.get('isLoaded')) { return 'success'};
return 'error';
}
export default Ember.Handlebars.makeBoundHelper(bootstrapModelStatus,
'isDeleted', 'isError', 'isValid', 'isSaving', 'isNew', 'isDirty', 'isLoaded'
);
Related
This is how I get all data in config.js:
this.get('/rentals', function (schema, request) {
if (request.queryParams.value) {
// The code should be here...
} else {
return schema.rentals.all();
}
}
I looked at documentation, but there's no way to get filtered ones. Apparently there are commands like all(), find(), findBy(), create(), etc. But there's nothing that filters out and returns. Any help?
Figured out: filter could be used with all().
this.get('/rentals', function (schema, request) {
if (request.queryParams.value) {
let filteredRentals = schema.rentals.all().filter(function (i) {
return i.attrs.value.toLowerCase().indexOf(request.queryParams.value.toLowerCase()) !== -1;
});
return filteredRentals;
}
return schema.rentals.all();
});
I am getting the error from the title when I attempt to run relay-compiler in my project. I am using graphql-python/graphene-django for the back end graphql server. Here's an abbreviated copy of my schema.
grove.gql_schema.py:
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Tree
class TreeNode(DjangoObjectType):
class Meta:
model = Tree
filter_fields = ['owner']
interfaces = (relay.Node,)
class Query(ObjectType):
def resolve_my_trees(self, info):
if not info.context.user.is_authenticated:
return Tree.objects.none()
else:
return Tree.objects.filter(owner=info.context.user)
my_trees = DjangoFilterConnectionField(TreeNode)
tree = relay.Node.Field(TreeNode)
project.gql_schema:
class Query(grove.gql_schema.Query, graphene.ObjectType):
pass
schema = graphene.Schema(query=Query)
With that setup, I can successfully run the following query in GraphiQL
query {
myTrees {
edges {
node {
id
}
}
}
}
So far so good, but now I'm trying to build a client-side component that can use this query.
jsapp/components/index.jsx:
import React from 'react';
import {graphql, QueryRenderer} from 'react-relay';
import environment from '../relay_env'
const myTreesQuery = graphql`
query componentsMyTreesQuery {
edges {
node {
id
}
}
}
`;
export default class App extends React.Component {
render() {
return (
<QueryRenderer
environment={environment}
query={myTreesQuery}
variables={{}}
render={({error, props}) => {
if (error) {
return <div>Error!</div>;
}
if (!props) {
return <div>Loading...</div>;
}
return <div>User ID: {props.edges}</div>;
}}
/>
);
}
}
The query is identical, but when I run relay-compiler --src ./jsapp --schema ./schema.graphql --extensions js jsx I get the error:
GraphQLParser: Unknown field `edges` on type `Query`.
Source: document `componentsMyTreesQuery` file: `components/index.jsx`.
I get this error if when I use the .json schema generated by the Django graphaql_schema management command or the .graphql one retrieved by get-graphql-schema.
What part am I missing?
I might be a bit late but it looks to me like you're trying to ask for the edges on the root of your schema, the fragment:
const myTreesQuery = graphql`
query componentsMyTreesQuery {
edges {
node {
id
}
}
}
`;
I think could be altered to:
const myTreesQuery = graphql`
query componentsMyTreesQuery {
myTrees {
edges {
node {
id
}
}
}
}
`;
The text after the keyword query refers to the name of the 'query', not the root of the schema. If you're using Relay Modern (which by the use of QueryRenderer, I assume you are) then the compiler should yell at you to fix the name of your fragment to something that reflects the filename that it's stored in.
I want to write a function (in a jenkins job DSL) which returns a reusable set of parameters for job defintions. Like this:
def pars(name) {
return parameters {
booleanParam(name, true)
}
}
pipelineJob("Test1") {
pars("name")
}
pipelineJob("Test2") {
pars("name2")
}
This does not work, but can I somehow rewrite the example so that it does work?
parameters can not be called in your helper method, but you can create a closure in a helper method.
Try this:
def pars(name) {
return {
booleanParam(name, true)
}
}
pipelineJob("Test1") {
parameters pars("name")
}
Or this:
def pars(name) {
return {
parameters {
booleanParam(name, true)
}
}
}
pipelineJob("Test1").with pars("name")
I have the following:
// In the service
me: Ember.computed('token', function() {
return this.container.lookup('service:store').findRecord('user', 63);
}
// In the template - Works
{{this.currentSession.me.profile.firstName}}
Above works as expected. However, if I replace findRecord with query and throw it a filter param:
// In the service
me: Ember.computed('token', function() {
return this.container.lookup('service:store').query('user', { filter: { token: this.get('token') } });
}
// In the template - Works
{{this.currentSession.me.lastObject.profile.firstName}}
The lastObject is required, since the result from query is a collection. I am wondering if it is possible to get this work without doing lastObject. i.e.
{{this.currentSession.me.profile.firstName}}
I tried to put the lastObject in the computed property, like:
return this.container.lookup('service:store').query('user', { filter: { token: this.get('token') } }).lastObject;
To no avail. Any thoughts?
Try this:
return this.container.lookup('service:store').query('user', { filter: { token: this.get('token') } }).then(function(users){
return users.get('lastObject');
});
I'm trying to search for all employees that have a title of developer
As per the documentation (http://guides.emberjs.com/v1.10.0/models/finding-records/) It seems the correct way to do this is:
return this.store.find('employee', { title: "developer" });
But this is not working in Ember CLI 0.2.2, and I can't even see my template when I try this, even though when I do
return this.store.find('employee')
I can see a list of all employees and there are multiple employees with that title
Turns out I needed to override the DS.FixtureAdapter::queryFixtures method. I went into my adapters/application.js file and added
queryFixtures: function(records, query, type) {
return records.filter(function(record) {
for(var key in query) {
if (!query.hasOwnProperty(key)) { continue; }
var value = query[key];
if (record[key] !== value) { return false; }
}
return true;
});
}