i´m using papa's course ccjs code investigate breeze.js , spa. using code i´m trying manage aditional information cames server not entity contained in metadata cames entityframework.
so created no-db class called esto , server method lookups:
[httpget] public object informacion() { var = new esto(....); var b = new esto(.....); var c = new esto(......); return new {a,b,c}; }
then in model.js inside configuremetadatastore call:
metadatastore.addentitytype({ shortname: "esto", namespace:"codecamper", dataproperties:{ id: {datatype: breeze.datatype.int32,ispartofkey: true}, name: {datatype: breeze.datatype.string} } };
and define in model entitynames array: esto:'esto' entity
now in context.js load creating server side method getlookups called getinformacion:
function getinformacion(){ return entityquery.from('informacion') .using(manager).execute() }
and inside primedata in success method call this:
datacontext.informacion = { esto: getlocal('esto',nombre)};
where getlocal is:
function getlocal(resource, ordering) { var query = entityquery.from(resource).orderby(ordering); return manager.executequerylocally(query); }
i error in query contained in getlocal states can not find entitytype either entitytypename: 'undefined' or resourcename:'esto'.
what i´m doing wrong?
thanks
you there! :-) had specified target entitytype
in query think have worked.
try this:
var query = entityquery.from(resource).orderby(ordering).totype('esto');
the totype()
method tells breeze top-level objects returned query of type esto
.
why?
let's think how breeze interprets query specification.
notice began query, do, naming resource supply data. resource typically path segment remote service endpoint, perhaps name of web api controller method ... method named "foos".
it's critical understand the query resource name same entitytype
name! may similar - "foos" (plural) similar type name "foo" (singular). resource name else. "getfoos" or "greatfoos" or @ all. matters service method returns "foo" entities.
breeze needs way correlate resource name entitytype
name. breeze doesn't know correlation on own. totype()
method 1 way tell breeze it.
why remote queries work without totype()
?
you don't add totype()
queries. why now?
most of time [1], breeze doesn't need know entitytype
until after data arrive server. when json query results includes type name (as when come breeze web api controller example), breeze can map arriving json data entities without our ... assuming these type names in metadata.
local cache queries different
when query cache ... executequerylocally
... breeze must know cached entity-set search before can query locally.
it "knows" if specify type totype()
. if omit totype()
, breeze has make query's resource name.
breeze doesn't guess. instead, looks in entitytype/resourcename map entity-set matches query resource name.
the resource name refers service endpoint, not cached entity-set. there no entity-set named "informacion", example. breeze uses entitytype/resourcename map find entity type associated query resource name.
entitytype/resourcename
the entitytype/resourcename map 1 of items in breeze metadatastore
. you've never heard of it. that's good; shouldn't have think ... unless unusual define own types.
the map of new metadatastore
starts empty. breeze populates server metadata if metadata contain entitytype/resource mappings.
for example, breeze efcontextprovider
generates metadata mappings derived dbset
names. when define foo
class , exposed dbcontext
dbset
named "foos", efcontextprovider
metadata generator adds mapping "foos" resource name foo
entity type.
controller developers tend use dbset
names method names. conventional breeze web api controller "foo" query method looks this:
[get] public iqueryable<foo> foos() {...}
now if take query such this:
var query = entityquery.from('foos').where(...);
and apply cache
manager.query.executelocally(query).then(...);
it works.
why? because
- "foos" name of
dbset
on server - the
efcontextprovider
generated metadata mapping ["foos"model.foo
] - the web api controller offers
foos
action method. - the breezejs
query
specifies "foos" - the
executelocally
method finds ["foos"-to-model.foo
] mapping in metadata , applies query entity-setfoo
.
the end-to-end conventions work silently in favor.
... until mention resource name not in entitytype/resourcename map!
register resource name
no problem!
you can add own resource-to-entity-type mappings follows:
var metadatastore = manager.metadatastore; var typename = 'some-type-name'; var entitytype = metadatastore.getentitytype(typename); metadatastore.setentitytypeforresourcename(resource, entitytype);
breeze happy name of type:
metadatastore.setentitytypeforresourcename(resource, typename);
in case, somewhere near top of datacontext
, write:
var metadatastore = manager.metadatastore; // map 2 resource names esto metadatastore.setentitytypeforresourcename('esto', 'esto'); metadatastore.setentitytypeforresourcename('informacion', 'esto');
don't over-use totype()
the totype()
method short-cut solution when need map top-level objects in query result entitytype
. don't have mess around registering resource names.
however, must remember add totype()
every query needs it. configure breeze metadata resource-to-entity-type mapping , you'll desired behavior every time.
notes
[1] "most of time, breeze doesn't need know entitytype
until after data arrive server." 1 important exception - out of scope discussion - when query filter involves date/time.
Comments
Post a Comment