Breeze does not generate ko observables

爷,独闯天下 提交于 2019-12-10 20:19:14

问题


I have the following problem with a simple MVC4 + ko + breeze webapp: the breeze entities returned by a query are simple javascript objects, without ko observables. I inspected the todo sample (which correctly returnes ko observables) and I didn't find the place where breeze is configured to work with ko (and generate observables). I tried adding the following lines, but nothing changed:

var core = breeze.core;
var entityModel = breeze.entityModel;

core.config.setProperties({
    trackingImplementation:     entityModel.entityTracking_ko,
    remoteAccessImplementation: entityModel.remoteAccess_webApi
});

Thanks in advance for Your kind help


回答1:


@frenchfraso - It may be worth knowing of a few improvements in Breeze since you wrote your code.

The entityModel namespace is deprecated and everything that was on it has been elevated to breeze. The entityModel namespace still works ... but you want to get rid of it when you have the time. Here's how you'd create an EntityManager today:

  var manager = new breeze.EntityManager(serviceName);

Knockout is now the default "modelLibrary" adapter and Web API is the default "dataservice" adapter so you no longer have to configure breeze core.

That means you can simply delete every line of the code in your question :)

There is a new syntax to specify a non-default Breeze adapter. Here's an example that configures Breeze to use the Backbone model library instead of Knockout:

  breeze.core.config.initializeAdapterInstances({modelLibrary: "backbone"});



回答2:


My problem was that I have loaded knockout AFTER breeze and therefore breeze returned me POJO objects.

<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/q.js"></script>
<script src="Scripts/breeze.debug.js"></script>    
<script src="Scripts/knockout-2.1.0.debug.js"></script>
<script src="Scripts/toastr.js"></script>
<script src="Scripts/require.js" data-main="App/main"></script>    

After changing the load order breeze returned KO observables:

<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/q.js"></script>
<script src="Scripts/knockout-2.1.0.debug.js"></script>
<script src="Scripts/breeze.debug.js"></script>    
<script src="Scripts/toastr.js"></script>
<script src="Scripts/require.js" data-main="App/main"></script>    



回答3:


gotcha! the problem was in a couple of missing attributes in the WebApi controller:

[JsonFormatter, ODataActionFilter]
public class MyController : ApiController
{
    // my methods...
}

After adding [JsonFormatter, ODataActionFilter] the returned entities had the expected ko observables!



来源:https://stackoverflow.com/questions/13582107/breeze-does-not-generate-ko-observables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!